Menu

Post image 1
Post image 2
1 / 2
0

Explained Global, Local & Block Scope in JavaScript

DEV Community·Nikita Maharana·19 days ago
#fglsgdyg
Reading 0:00
15s threshold

Scope: Scope generally means the visibility and accessibililty of variables in different parts of your code. Understanding of differnt types of scopes helps us write error free and clear code. Global Scope: Global scope means the variable is having its accessibility anywhere in the program, inside function or blocks etc. But sometimes it leads to naming conflicts. e.g: let global = "I'm a global variable"; function display(){ console.log(global); } display(); // "I'm a global variable" In this example, global is declared in the global scope and can be accessed inside the display function. Local Scope: The variables are only accessible within a function not outside of it. e.g: function local(){ var local = "I'm a local scope variable"; if (true){ console.log(local);// Works here also inside of if block } } Block Scope: The variables are accessed within that block not outside of it.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More