Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between const and var

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 205
    Comment on it

    Hello Readers! In this blog we will be discussing about the different identifiers. What is the difference between them and when to use which identifier? First lets start with the const.

     

     

    Const : is used to declare a variable whose value cannot be changed after it has been initialized. The value of the variable remains unchanged during re-assignment and also it can not be redeclared.

    // define MYVARIABLE1 as a constant and assign value 10 to it
    
    const MYVARIABLE1 = 10;
    
    // following line will throw an error 
    
    MYVARIABLE1 = 20;
    
    // will print 10 as output
    
    console.log( The value of MYVARIABLE1 is: + MYVARIABLE );
    
    //  redeclaration of a constant will throw an error
    
    const MYVARIABLE1 = 20;
    

     

    var : variable declared with the var keywords can be redeclared and re-assigned, that we can change the value of the variable which is declared using the var keyword.

     

    var x =10;
    
    var y =20;
    
    var sum = x+y;
    
    console.log(sum);  //   prints 30 as output
    
    
    x= 30;   // redeclaration of variable x
    
    sum =x+y;
    
    console.log(sum);  // print 50 as output

     

    Happy Coding :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: