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.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay