DEV Community

Max Lockwood
Max Lockwood

Posted on • Updated on • Originally published at maxlockwood.dev

What are the Three Variables in JavaScript?

There are three ways to declare a variable and give it a value in modern JavaScript:

  • var
  • let
  • const

A variable is a container for a value, such as a string or a number we might use in a statement or as part of a sum.

var

Back when JavaScript was first created, this was the only way to declare variables.
When declaring variables in earlier versions of JavaScript, the var keyword was used instead of let:

var name = Andrew;
Enter fullscreen mode Exit fullscreen mode

The current method of declaring variables, let, has a number of benefits over var.
For instance, var permits re-declaration of variables with the same name, whereas let generates an error.

It is recommended to use let instead of var, when declaring variables.

let

The let keyword was introduced in ES6 (2015). A block scoped variable in JavaScript is declared with the let keyword. Variables defined using the let keyword are block scoped in contrast to those declared using the var keyword, which is often used to declare a variable in JavaScript and is handled as a standard variable.

Variables are created using the let keyword like this. Creating a variable in JavaScript is called “declaring” a variable:

let name;
Enter fullscreen mode Exit fullscreen mode

name is the name of the variable

After creating the variable, we can initialise it with a value:

name = 'Tom';
Enter fullscreen mode Exit fullscreen mode

The equal sign (=) is used to assign a value to our variable.

We can also assign our variable a value during creation, like this:

let name = 'Tom';
let age = 34;
Enter fullscreen mode Exit fullscreen mode

Remember, that we need to enclose text values in quotes.

After initialising a variable, we can output its value, using a method called console.log:

let name = 'Tom';
console.log(name);
Enter fullscreen mode Exit fullscreen mode

Variables can change their value during the program.

let age = 34;
age = 24;
console.log(age);
Enter fullscreen mode Exit fullscreen mode

Updating the value of a variable can be done as many times as needed.

const

The const keyword was introduced in ES6 (2015). Constants are similar to variables and are declared using the const keyword:

const color = 'green';
console.log(color);
Enter fullscreen mode Exit fullscreen mode

Constants must have a value when declared and they cannot change their value.

Constants are very useful. If you use const, it tells anyone looking at your code that this name will never be assigned to a different value.

Things to note about Constants:

  • Variables defined with const cannot be Redeclared
  • Variables defined with const cannot be Reassigned
  • Variables defined with const have Block Scope

If you want to learn more about JavaScript variables, MDN Web Docs is a wonderful resource – Storing the information you need — Variables

Top comments (0)