DEV Community

JavaScript: Introduction to Scope (function scope, block scope)

Ming-Shiuan Tsai on November 27, 2018

What is Scope? Scope determines the visibility or accessibility of a variable or other resource in the area of your code. Glob...
Collapse
 
nguyentuan1696 profile image
Tuan Nguyen •

Great clear explain !!!

Collapse
 
profgreatwonder profile image
Prof Great Wonder •

Great article. Really helped me with understanding the block scope

Collapse
 
nvs16 profile image
N V S ABHISHEK •

Nice article.

Collapse
 
mdsameershaikh profile image
mdsameershaikh •

Hi if you not have written {curly brackets} for explaining block scope, I could not understand it.

thanyou verymych for expaining in such easy language.

Collapse
 
timrodz profile image
Juan Alejandro Morais •

Great article - thanks for sharing! :)

Collapse
 
tmsravan profile image
TMsravan •

Nice explanation

Collapse
 
sashankramaraju profile image
Sashank Ramaraju •

Very nice article on scopes !!!

Collapse
 
siddh profile image
Siddh •

If we are not able to reassign the value in the variable that is defined with the const
then why it is changing his value every time, when the loop gets executed

const array = [1,2,3,4] ;

for (let index = 0; index < array.length; index++) {
const element = array[index];
console.log(element);
}

answer is

1
2
3
4

Collapse
 
devhamzaa profile image
Dev Hamza •

you are storing the array[index] in element
you are not re-declaring but you are updating the value of element

Collapse
 
navidkhm profile image
M.Navid •

Thank you @mingt .
I think there is a misinformation In "Lexical Scope" topic that you define it: "children scope have the access to the variables defined in the parent scope."
I think that definition is for "Scope Chain" and the "Lexical Scope" related to which type of referencing to parent in Scope Chain does JS (Lexical or Dynamic).

Collapse
 
gidoskales profile image
GidoSkales •

Well Orchestrated ....

Collapse
 
saroj8455 profile image
Saroj Padhan •

very nice explanation

Collapse
 
sanjarcode profile image
Sanjar Afaq • • Edited

This is really helpful, embed this picture as a summary if you want.

Collapse
 
devhamzaa profile image
Dev Hamza •

thanks dude i used it in my notes

Collapse
 
mhamadsiro profile image
Mohamad-serhan •

Fantastic, great job

Collapse
 
vickcharles profile image
Vikler Charles •

Very helpful article! :)

Collapse
 
banzaman profile image
Mark Rubanza •

nice explanation
salut

Collapse
 
b77748 profile image
B77748 •

Thank you, it's very clear now.

Collapse
 
hafedbenchellali profile image
Hafed Benchellali •

Thank you I Finally understood !

Collapse
 
gauravgupta799 profile image
Gaurav Gupta •

_Easy and Clear explanation: thanks for sharing this article. _

Collapse
 
mylifewithmyexperiment profile image
SHASHI JAISWAL •

very very clear and vivid explanation. awesome

Collapse
 
saroj8455 profile image
Saroj Padhan •

Nice explain of the scope

Collapse
 
jfeijo profile image
João Antônio Feijó •

Very cool explanation, straight to the point. I'm currently learning the scope subject on my bootcamp and you've made me understand it a lot better!

Collapse
 
anhaltjan profile image
Jan Anhalt •

This was very helpful, thank you very much! :)

Collapse
 
tuvudu profile image
Tuyen Vd. •

thank for your explantation

Collapse
 
umeredava profile image
Umar Sanil •

Good

Collapse
 
jyotimoytrain profile image
Jyotirmoy Deb •

is block scope and child scope same ?

Collapse
 
johnkibirige005 profile image
Kibirige John •

Thank you so much for the elaborative write-up

Collapse
 
seven07del profile image
Prince Djangmah •

Awesome, simplified and tactical. Loved it.

Collapse
 
bokooutman profile image
outman boko •

Thank you so much for that explanation

Collapse
 
pervez profile image
Md Pervez Hossain •

Very much informative Article About Scope in JavaScript. Well Define.
Thank you

Collapse
 
akhrorvaliev profile image
Oshie • • Edited

I have not seen any simple explanation of function scope and block scope before. It is a worthwhile article to read, thanks

Collapse
 
yusufnviiri profile image
yusufnviiri •

Thank you for such Great work

Collapse
 
mahmoudabdulmuty profile image
Mahmoud Abdulmuty •

Good Explanation .. thanks alot

Collapse
 
kyuhyunhan profile image
KH •

clear, helpful description of scope! Thanks!