Some concepts of JavaScript

Asif Mahmud
4 min readMay 6, 2021

Hoisting: Hoisting is a concept in JavaScript which allows to access a variable before it is even declared. We can declare a hoisted variable by using ‘var’. Let’s look at an example:

x = 5;
console.log(x); //output 5
var x;

When we declare a variable using var, what JavaScript does is it takes the variable and declares it at the start of the block. That’s why we can access the hoisted variable. Var also supports multiple declaration. That means we can declare the same variable multiple times and it will still be valid code.

Block-Level Declarations: When we declare variables we can’t just access them from anywhere. It maintains certain condition. It mainly depends on how the variable was declared and where it was declared. Usually a variable can be accessible from the block of code it was declared into. We can specify a block using parenthesis for eg. (), {}. Let’s have a look into an example:

function test(){
var x = 5;
console.log(x); //output: 5
}
console.log(x); //shows error

You can access variables declared in the global scope:

var x = 5;
function test(){
console.log(x); //output: 5
}
console.log(x); //output: 5

Error handling in JavaScript: Errors are a common scenario in programming. There might be several reasons for getting errors. It can be user generated (giving an invalid input) or maybe there was a problem in the logic. In any case the programmers have to be prepared for that. If the programmer doesn’t write code that will be able to handle errors then the program will crash and thus defeats the purpose of the program. So handling error is essential. We can handle errors using try-catch block in JavaScript. The syntax is given below:

try{
//write your code
}
catch(err){
//handle errors
}

Arrow functions: ES6 has added some new features in JavaScript. Arrow function is of them. It makes it easier to declare and use functions. Plus it makes the code shorter and more readable. The basic syntax is like this:
const functionName = parameter=> yourCode
Let’s look at an example:

const square = x => x*x;
console.log(square(2)) //output: 4

The above example returns the square of a number. Whatever your expression is after the ‘=>’ will be returned by default. This is the case only when your expression is a single line. If you write multiple lines of code then you have to specifically return a value. You can also pass multiple parameters by adding parenthesis.

const sum = (arr) => {
let result = 0;
for(let i = 0; i < arr.length; i++){
result += arr[i];
}
return result;
}
console.log(sum([1,2,3,4])); //Output: 10

for in loop: Another way to iterate through an array or an object is using for in loop. The syntax is like this: for(iterator in array/object){ //your code}. Example:

var arr = [1,2,3,4]
for(x in arr){
console.log(arr[x]) //output: 1 2 3 4
}

var obj = {
name: “Karen”,
age: 25
}
for(x in obj){
console.log(obj[x]) //output: Karen 25
}

Array.find() method: If you want to search for a conditional value in an array then find() method might just be the thing you are looking for. It iterates through an array and returns the first value that meets the test function condition. For example:

var arr = [1,2,3,4,5];
var result = arr.find(myFunction);
function myFunction(value, index, array) {
return value > 3;
}
console.log(result); // output: 3

Array.findIndex() method: This method is also similar to the find() method. But instead of returning the value it returns the index of the array element. Example:

var arr = [10,20,30,40,50];
var result = arr.find(myFunction);
function myFunction(value, index, array) {
return value > 20;
}
console.log(result); // output: 2

String.trim() method: There are times when we might need to remove the white spaces from a string. Suppose we are getting user’s email input and the user entered an email with white spaces around it. Then we can do something like this:

var email= “ example@example.com “;
console.log(str.trim()); //output: “example@example.com”

Array.every() method: It iterates through the elements of an array and checks if the values meet the condition which it gets from a function. Then it returns true if all the values pass the condition and pass false even if one value fails to do so. Let’s look at an example:

const lessThanFive = (elementValue) => elementValue < 5;
const arr = [1,2,3,4];
console.log(lessThanFive); //output: true

const lessThanFive = (elementValue) => elementValue < 5;
const arr = [1,2,3,4,5];
console.log(lessThanFive); //output: false

Array.some() method: It works same as Array.every method. The difference is it returns true even if only one value meets the condition and returns false if every value fails to meet the condition. Let’s look at an example:

const lessThanFive = (elementValue) => elementValue < 5;
const arr = [4,5,6,7,8];
console.log(lessThanFive); //output: true

const lessThanFive = (elementValue) => elementValue < 5;
const arr = [6,7,8,9,10];
console.log(lessThanFive); //output: false

--

--