In this tutorial, you will learn about JavaScript variables and constants, and furthermore how to instate and use them with the help of examples.

JavaScript Variables

In programming, a variable is a holder (storage area) to hold information. For example,

let num = 5;

Here, num is the variabe that holds the number 5.


JavaScript Declare Variables

In JavaScript, we use the accompanying watchwords to pronounce variables: var and let. For instance,

var x;
let y;

Here, x and y are variables.


JavaScript var Vs let

Both var and let are used to declare variables. Be that as it may, there are a few differences between them.

varlet
var is used in the older versions of JavaScriptlet is the new way of declaring variables starting ES6 (ES2015).
var is function scoped (will be discussed in later tutorials).let is block scoped (will be discussed in later tutorials).
For example, var x;For example, let y;

Note: It is recommended we use let instead of var. However, there are a few browsers that do not support let. Visit JavaScript let browser support to learn more.


JavaScript Initialize Variables

We use the assignment operator = to allocate an incentive to a variable.

let x;
x = 5;

Here, 5 is assigned to variable x.

You can likewise initialize variables during its announcement.

let x = 5;
let y = 6;

In JavaScript, it’s possible to declare variables in a single statement.

let x = 5, y = 6, z = 7;

If you use a variable without instating it, it will have an undefined value.

let x; // x is the name of the variable

console.log(x); // undefined

Here x is the variable name and since it doesn’t contain any worth, it will be undefined.

You will learn about undefined and other data types in the next tutorial in detail.


Change the Value of Variables

It’s conceivable to change the value stored in the variable. For instance,

// 5 is assigned to variable x
let x = 5; 
console.log(x); // 5

// vaue of variable x is changed
x = 3; 
console.log(x); // 3

The value of a variable may vary. Hence, the name variable.


Rules for Naming JavaScript Variables

The rules for naming variables are:

  1. Variable names should begin with either a letter, an underscore _, or the dollar sign $. For instance,
//valid
let a = 'hello';
let _a = 'hello';
let $a = 'hello';

2. Variable names cannot start with numbers. For example,

//invalid
Let 1a = 'hello'; // this gives an error

3. JavaScript is case-sensitive. So y and Y are different variables. For example,

let y = "hi";
let Y = 5;

console.log(y); // hi
console.log(Y); // 5

4. Keywords cannot be used as variable names. For example,

//invalid
let new = 5; // Error! new is a keyword.

Notes:

Despite the fact that you can name variables in any capacity you need, it’s a decent practice to give an enlightening variable name. If you are using a variable to store the number of apples, it better to use apples or numberOfApples as opposed to x or n.

In JavaScript, the variable names are generally written in camelCase if it has different words. For instance, firstName, annualSalary, and so forth


JavaScript Constants

The const keyword was also introduced in the ES6(ES2015) version to create constants. For example,

const x = 5;

Once a constant is initialized, we cannot change its value.

const x = 5;
x = 10;  // Error! constant cannot be changed.
console.log(x)

Simply, a constant is a type of variable whose value cannot be changed.

Also, you cannot declare a constant without initializing it. For example,

const x;  // Error! Missing initializer in const declaration.
x = 5;
console.log(x)

Note: If you are certain that the value of a variable won’t change all through the program, it’s prescribed to use const. In any case, there are a couple of browsers that don’t support const. Visit JavaScript const browser support to learn more.


Now that you know about variables, you will learn about different types of data a variable can store in the next tutorial.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.