JavaScript Block Bindings And Functions

Z anika Nibir
2 min readMay 6, 2021

--

Var Declarations

Var” is a keyword which is used in javaScript making declaration a variable traditionally.With var we can declare many time as want.Var support function scope or global scope.It ignores code blocks.

var price = 20Here we declare a variable with value 20.

Var Hoisting

In Hoisting is a practice in javaScript which means declare a variable and function at the top of the code where the actual declaration happened.

Here,

console log(hello)var hello = nibir;Output: hello is undifined;

That means variale are hoisted at the top of the declaration and we find output undifined.

Block-Level Declarations

Block-Level Declarations means declare variables is only avaiable in given block.We can explain about block in javascript anything within curly braces { } is a block.That means declared variable is unattaiable outside the block.We know a keyword in javascript which is block scoped.Now we will see an example given bellow

let say = “good morning”;let day = 3
if(day>2){let say = “good noon”;console.log(say);Output:”good noon”}
console.log(say)
Output:say is undifined

Block Binding in Loops

If we declare a variable with var it will be hoisted top of the function.So If we declare a variable within a for loop.it will be able outside of the loop.On the other side when we declare a variable with let and const it will be unattainable outside the loop

Global Block Bindings

In javaScript there has let, var& const which are used to declare variable.In these var is different from let&const.In these var is used In global scope on the other hand let & and const can’t.

Emerging Best Practices for Block Bindings

We can use let instead of var in variable declaration.it is a update practice.In protected declaration we could use const.Nowadays .we could use const by default and only use let when you know a variable's value needs to change.

Function with default parameter value

The function with default value explain that if there has no value that it will use default value.

Here,

function add(x, y=1){return x*y
}
console.log(add(2 5));Output:7console.log(add(5));Output:6

in this example we can explore that if there has no given default value of y.then the 2nd output will be undifined.

The Spread Operator

In javascript we can differ in 2 values,That means we find max value from 2 values easily but in array when it is many values than we use the spread operator.

Here,

const number = [12,10,14,100]console.log(Math.max(…number))Output:100

Arrow Functions

It is a form of shorter function in javascript.ES6 introduce us with this.when we use functions for one statement.


const hellow =()=>”say hellow”;

Block-Level-Function

The function declare in a single block.That means it only works in curly braces{}.

Here,

function fruits(){var fruit = ‘mango’console.log(fruit)Output:mango}console.log(fruit)Output:undifined

--

--