Piyas talukder
4 min readMay 6, 2021

--

  1. Function default parameter: It allows parameters to be initialized with default values if we do not provide any value then its value becomes undefined. For example :
function greetings(name){
console.log("Welcome mr. " + name);
}
greetings('Hamidul');

So the output is Welcome mr. Hamidul.

If we didn’t pass any value as argument of greetings function

function greetings(name){
console.log("Welcome mr. " + name);
}greetings();

Output is Welcome mr. undefined

We can see that the name value is undefined. Now we initialize a default value parameter of the function greetings like below

function greetings(name='Kuddus Mia'){console.log("Welcome mr. " + name);}greetings();

Now the output is Welcome mr. Kuddus Mia .

We set a default value of the name parameter.

2. Arrow Function: ES6 introduced to write shorter function syntax called arrow functions.

let greetings =()=> console.log('Welcome to my website');greetings();

Output: Welcome to my website

Arrow functions with parameter

let greetings =(name)=> console.log('Welcome to my website '+name);greetings('Hamidul');

Output: Welcome to my website Hamidul

3. The Spread Operator: ES6 has introduced another new feature into javascript called spread operator three-dot (…) is used to expand or spread an iterable or an array. For example,

Concat array using the spread operator.

let arr1=['1','2','3'];let arr2=['7','8','9'];let result=[...arr1,...arr2];console.log(result);

Output: [ ‘1’, ‘2’, ‘3’, ‘7’, ‘8’, ‘9’ ]

Example of spread operator with objects

const user1={name: 'John Doe',age:32}const user2={name: 'Jen Doe',location:'Copenhagen'}const mergeUsers={...user1,...user2};console.log(mergeUsers);

Output: { name: ‘Jen Doe’, age: 32, location: ‘Copenhagen’ }

4. Scope in javascript: Scope defines in javascript allows accessibility of variables, objects, and functions in the area of your code.

5. Global Scope: In a javascript document, a variable declared outside function is called a global variable and can be accessed from anywhere in the document.

const name ='Hamidul';function greetings(){console.log('Welcome to mr. ' + name);}greetings();

Output: Welcome to mr. Hamidul

In this code, we declare the name variable outside the greetings function and we used it inside. Here the name variable is global.

6. Local scope: Variables declared inside a function become local scope. A local scope can be divided into two types functional scope and block scope.

7. Functional Scope :

Declare a variable inside a function is called a functional scope variable. Outside of the function, can’t access it. Var keyword is used to define functional scope.

function greetings(){var name ='Hamidul';console.log('inside function acccess : ',name);}greetings();

Output: inside function access: Hamidul

But if we code something below

function greetings(){var name ='Hamidul';console.log('inside function acccess : ',name);}greetings();console.log(name);

Here we want to print name variable outside function and getting an error

console.log(name);             ^ReferenceError: name is not defined

8. Block Scope :

Block scope is defined as the aria inside for, while loops and if, switch conditions. We can simply say that something inside the {curly bracket} is called a block. In ES6 introduce two keywords const and let to allow the programmer to declare a variable for block scope. Declare variable const or let can’t be accessed outside the block.

function info(){if(true){var name = 'Hamidul';        //function scopeconst email = 'hamiduldiu@gmail.com';     // block scopelet address = 'Copenhagen, Denamrk';   // block scope}console.log(name);console.log(email);console.log(address);}info();

In this given example we can only access name but can’t access email and address.

9. Lexical Scope: Suppose we have two functions One is a parent and inside parent another child functions. The child function that can access the variable defined in the parent function is called lexical scope.

function info(){var name = 'Hamidul';        //function scopeconst email = 'hamiduldiu@gmail.com';     // block scopelet address = 'Copenhagen, Denmark';   // block scopefunction myInfo(){console.log('Inside child function :' + name);console.log(email);console.log(address);}myInfo();}info();

Output:

Inside child function: Hamidul

hamiduldiu@gmail.com

Copenhagen, Denmark

10. Hoisting: Hoisting in javascript refers to variable and function decoration that are lift or raise to the top of your code. This simple meaning that you can reference them before they are declared because they are raised to the top and the javascript engine knows it exists and not getting any error. Let’s take some examples to illustrate it. Here is the simple code below

exampleHoisting();function exampleHoisting(){console.log('Before declare variable email : '+ email);var name ="Md. Hamidul Islam";console.log(name);var email='hamiduldiu@gmail.com';console.log('Aftre declare variable email : '+ email);

}
Output : Before declare variable email : undefined

Md. Hamidul Islam

After declare variable email : hamiduldiu@gmail.com

A function decoration is hoisted to the top. We don’t have to declare the function first and called it down here. But what happened the first console log it shows Before declare variable email : undefined . it gives us undefined but didn’t give us any error. Normally if the variable doesn’t exist we receive an error. Because javascript engine knows that variable email exists, however, the result is undefined. Because email value is assigned in down. For this reason, simple print undefined.

Another example: This time I am creating function expression, not function declaration.

sum(5,6);let sum = function(a,b){console.log(a+b);}

Output : sum(5,6);

ReferenceError: Cannot access ‘sum’ before initialization

This time I am getting errors. Because javascript engine knows sum exists but it doesn’t know it is function. So with function expression call must be after declaration.

let sum = function(a,b){console.log(a+b);}sum(5,6);

Happy Coding !!! 😉 😉 😉 😉 😉 😉

--

--