DEV Community

Cover image for Hoisting,Scope in JavaScript
Dev Learner
Dev Learner

Posted on

Hoisting,Scope in JavaScript

Hoisting:
Hoisting is a default behavior of JavaScript. JavaScript always takes all its declarations first. It can be said that this is one of its habitual actions.And this behaviour is called hoisting.
Since it is hosted, we can call before declaring any function.
I am trying to explain the matter through the following example.

Here is Input:

numberSum();
function numberSum(){
var a=32;
var b=43;
var sum=a+b;
console.log(sum)
}

Here is Output:

75

Scope:

Scooping is one of the most important aspects of JavaScript. A declared function can be used in some places and not in other places. It can be accessed from any place. .
There are two two kinds of scoping.
1.Local Scope,
2.Global Scope,
Local Scope: Usually the variables declared inside the function are the local variables of that function.
The scope of such variables is local.This means that this variable can only be accessed inside the declared function. It cannot be used for any other function.
Global Scope: If a variable is not declared inside a function, then it is a global scope. This means that a variable declared anywhere outside the function is a global scope. Global variable can be accessed from inside any function or from any place.

Top comments (0)