DEV Community

Sasireka
Sasireka

Posted on

Variables

1.Variables in JavaScript

  • JavaScript variables are containers for data.

  • JavaScript variables can be declared in 3 ways: var, let, const

var:

  • The var keyword was used in all JavaScript code before 2015.

  • It can be redeclared and reassigned anywhere in the program.

  • var a = 10;
    var a = 20; //redeclared
    a = 30; //reassigned
    console.log(a) //output:30

let:

  • It can be reassigned but it cannot be redeclared in the program.

  • let a = 20;
    a = 30; //reassigned
    let a = 10; //redeclared(error)
    console.log(a) //output:30

const:

  • It cannot be redeclared and reassigned in the program.

  • Once we declared the variable using const, then we cannot able to redclared and reassigned the value.

  • const a = 30;
    a = 20; //reassigned(error)
    const a = 10; //redeclared(error)
    console.log(a) //output:30

Top comments (0)