Home Node JS var,let,const difference

var,let,const difference

4 min read
0
0
126

This tutorial will teach you the main difference between var,let,const step by step.

var let const difference

1.var is global scope.

2. let is block scope.

3.const is block scope.

variable declaration

let we discuss about each

var

var is global scope.

Output

12

i created the variable name mytest assign 12. you can see the output.

Output

undefined
0

above example i printed console.log(mytest); at the first time. no value assign thats way output display as undefined. same mytest name you can assign more than one value but when you print it out  last declared value 0 as been printed. so that output displayed as 0.

Output

undefined
509

above example console.log(mytests); at the first time. no value assign thats way output display as undefined.
after that if the particular condition correct only print the value 509. 1000 is grater than 500 condition is corrent then print it out 509.

let

let is block scope.

Output

120

Error

ReferenceError: Cannot access ‘mytestvar’ before initialization

you cannot call console.log(mytestvar); at the firstline you get the error.

Error

SyntaxError: Identifier ‘mytest’ has already been declared

Same name you cannot use again by again.

you must create the variable first at the firstline then only works because let is a block scope

Output as

7000

const

Output

12

Error

SyntaxError: Identifier ‘mytest’ has already been declared.

Same name you cannot use again by again.

Error

TypeError: Assignment to constant variable.

when we create a const variable value we can assign once cannot assign again

Output

Tutusfunny
Tutusfunny
[ 60, 20, 50 ]

 

 

 

 

 

 

 

 

 

    Load More Related Articles
    Load More By admin
    Load More In Node JS

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Check Also

    Create Maven project in Intellij

    In this tutorial will see how to create Maven project in Intellij step by step. First Step…