JavaScript Variable Declaration

JavaScript Variable Declaration

Table of contents

Variable

What is Variable ?

  • a]anything that can store value or some data

  • b] as name indicates it can vary

  • c]it is the location where data is stored

example: factorial = 45,v = "ram"

  • We can not use number as initial of variable name

  • We can not use any other sign except '$' as initial of variable name.

  • We can not use uppercase as initial of variable name.

In JavaScript we can declare variable by three ways:

1)'let': 'let' keyword is used before variable to store value temporarily. We can update the value of variable if 'let' keyword is used.

example:

            let y = 12                   //here 12 is stored in variable y
            console.log(y)          //12
            y = 34                       //Now the value of y becomes 34 updated value
            console.log(y)          //34

2)'const' : 'const' keyword is used before variable to store value permanently. We can not update the value of variable if 'const' keyword is used before it.

example:

            const xy=10
            console.log(xy)     //10
            xy=20
            console.log(xy)     //error

3) 'var' : 'var' keyword is used to declare variable in window scope or global scope.The value of variable can be updated.

example:

            var a=24                
            console.log(a)                //24
            console.log(window.a)  //24  (run program on live server)
            a=10
            console.log(a)               //10 updated value