DEV Community

Lokesh
Lokesh

Posted on

JavaScript Variables .

What Are Variables in JavaScript?

Variables are used to store data such as names, numbers, and other values.

JavaScript provides three keywords to declare variables:

  • var
  • let
  • const

1. var

var is an older way to declare variables in JavaScript.

var name = "John";
console.log(name);
Enter fullscreen mode Exit fullscreen mode

Output:

John
Enter fullscreen mode Exit fullscreen mode

We can also reassign the value of a var variable.

var age = 20;
age = 25;

console.log(age);
Enter fullscreen mode Exit fullscreen mode

Output: 25

2. let

let is used when we want to declare a variable whose value may change.

let score = 50;
score = 80;

console.log(score);
Enter fullscreen mode Exit fullscreen mode

Output: 80

Unlike var, let cannot be declared again in the same scope using the same name.

3. const

const is used when we don't want to reassign a variable.

const country = "India";

console.log(country);
Enter fullscreen mode Exit fullscreen mode

Output: India

We cannot reassign country to another value. However, if a const variable stores an object or array, its contents can still be modified.

Top comments (0)