Follow us on: facebook twitter linkedin

PROGRAMING

Variable scope in Javascript

Post by : John Deo
Post date: 2024-12-08 13:46:00

Read time : 5 minute
Total views: 115 times
hero-img

Whats variable?

Variable is named of computer memory location. Which hold value in computer memory.

 

There are few reserved words for declare variable in JavaScript. These have mentioned in below

  1. let
  2. const
  3. var

let have local scope and global scope. Suppose u have declared name variable top of the program and same variable have declared in the function both will work correctly. Since they have different scope.

 

Same conditions will work for var and const. 

 

But difference is let and const have block scope. Dont have block scope for var.

 

Lets clear by an example. Suppose u have declared name variable top of the program and same variable has been using in the loop. There u will face value related problem because var dont have block scope. Which have in let and const and thats why arisen issue will solve by let and const .

Share by:

Keep exploring

Read what excites, achieves and moves us Read what excites, achieves and moves us