JavaScript Variable

Variable are used to storing data in single variable

JavaScript variable create three type

1- Using var keyword

2- Using let keyword

3-Using const keyword

Use var keyword - Generally var keyword indicate a variable which store data

Note : You can redeclare var keyword variable

Note : You can reassign var keyword variable

Example

/**********This is index.js file************/

var x = "Hello Word";

// You can print this variable

console.log(x);

var int = 1245751;

console.log(int);

Result

Hello Word

1245751

JavaScript Redeclare Variable

var int = 52;

console.log(int);

var int =16;

console.log(int);

Result

52

16

JavaScript Reassign Variable

You can reassign variable by this method

var int = 48;

console.log(int);

int = 26;  //without using var keyword 

console.log(int);

Result

48

26


JavaScript Let Keyword

You can reassign variable but can not redeclare variable in let keyword

Example

let int = 12;

console.log(int);

int = 16; // We reassign value int vaiable

console.log(int);

Result

12

16



JavaScript Const Keyword

You can not reassign and redeclare variable in const keyword variable

Example

cont int = 42.5;

console.log(int);

// you can not reassign and redeclare in const keyword variable

//if you you try to reassign and redeclare  it give error

/* * const int = 45;

console.log(int);  **/ 

/** int =46;

console.log(int); **/

// It also give an error


Related Articals

javascript file use