Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Block,Scope & Shadowing In javascript...
Jasmeet Singh Bali
Jasmeet Singh Bali

Posted on

     

Block,Scope & Shadowing In javascript...

Block?

                  # an empty block                  {                    //Compound statements                  }
Enter fullscreen modeExit fullscreen mode

IMPORTANT Block combines multiple statements as a single unit so as javascript can execute multiple lines of code as multiple lines inside block act as single unit

                  # no use of block single line possible only                  if(true) true;                  # with use of block                  # multiple lines can be written as a single unit where javascript expects only single statement                  if(true){                    const a =100; // line1                    console.log(100); //line 2                  }
Enter fullscreen modeExit fullscreen mode

Block Scope?

                {                  // variables or                  //functions that can be accessed inside this block                  // is called BLOCK SCOPE                }
Enter fullscreen modeExit fullscreen mode
  • When we run the code snippet-12, A Block scope is created where b and c are present while a is inside global scope even if it is declared inside a block

                # code snippet -12            {              var a = 10;              let b = 100;              const c = 1000;            }            console.log(a); // 10            console.log(b); //  referrence error            console.log(c); //  referrence error            # Inside debugger            # Block            b: undefined            c: undefined            # Global            a: undefined

that is the reason let and const are in block scope i.e seperate scope, and we can only access a in global scope while b and c in the block scope.

Shadowing in javascript

The variable declared inside the block takes preference i.e shadows the same named variable declared outside the block

              var a= 100;              {                var a=10;                console.log(a);              }              # output              10
Enter fullscreen modeExit fullscreen mode

In addition to shadowing the var keyword variable will modify the same name variable value declared in the global scope
var a = 100;
{
var a=10
console.log(a);
}
console.log(a)

              # output              10              10
Enter fullscreen modeExit fullscreen mode

Case : Let keyword

Shadowing happens in let also

However In contrast to var the let keyword declared variable do not alter the value of the same let keyword variable in the global scope

              let b = 100;              {                let b = 20;                console.log(b);              }              console.log(b);              # output              20              100              # debugger              # Script              b: 100              # Block              b: 20              # Global
Enter fullscreen modeExit fullscreen mode

IMPORTANT - Shadowing works the similar way for the function scope also

              const c=100;              function x(){                const c = 30;                console.log(c);              }              x();              console.log(c);              # output              30              100
Enter fullscreen modeExit fullscreen mode

I'llegal Shadowing

              # illegal shadowing              let a= 20;              {                var a =10;              }              # output              SyntaxError a has already been declared              # legal shadowing              # case-1              let b =20;              {                let b=2;              }              # output              No error runs perfectly              # case-2              var c =20;              {                let c=2;              }              # output              No error runs perfectly
Enter fullscreen modeExit fullscreen mode

IMPORTANT From the above code snippet the point to note is that shadowing can only happen if a particular variable do not cross the boundry for the case of illegal shadowing declaring a var a inside a block will give error as the var keyword will be in the global scope and not in the block scope

              let a= 20;              function x(){                var a =10;              }              # output              No error perfectly runs
Enter fullscreen modeExit fullscreen mode

IMPORTANT the arrow function behaves the same as normal function when Block Scope rules & shadowing are considered.

You My Friend Have A Great Day!!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
1122abhi profile image
Abhijeet Konnur
  • Work
    Developer
  • Joined

Thank you

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full Stack Engineer
  • Joined

More fromJasmeet Singh Bali

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp