Javascript Variables and Constants (with Examples)

In this tutorial, you are going learn about javascript variables and constants and how to initialize them with the help of multiple examples.

What are Javascript Variables

Javascript variables are the containers used to store data. Variables are the names given to computer memory locations in order to store data. For example,

Here, num is a variable. It’s holding 5.

How to Declare Javascript Variables

In javascript, we have two ways to declare variables first, we have the var keyword and second, we have the let keyword. We can use either var or let to declare variables.

Here, x and y are the variable names.

Difference Between Javascript var and let

varlet
var is an older way of declaring variables in javascript.let is a new way of declaring variables in javascript.
var is an ECMAScript1 feature.let was introduced in ES6.
var is functional scoped.let is block scoped.
For Example, var x;For Example, let y;

Note: It is recommended to use let to declare variables. However, some browsers do not support let. You can visit JavaScript let browser support for more information.

How to Initialize Javascript Variables

We use the assignment operator = to assign the value to a variable.

Here, we assigned 5 to variable x;

You can also initialize the variable on its declaration.

In Javascript, it’s also possible to declare variables in a single line.

If we use a variable without initializing it, then as a result we will get an undefined value;

let x; // variable x
console.log(x); // undefined

How to Change the Value of Variables

You can change the value stored in the variable. For Example, 

let x = 5;
console.log(x) // 5
x = 4;
console.log(x) // 4

In the above example, we changed the value of x from 5 to 4. The values ​​of variables can be changed that’s why we call them variables.

Rules for Making Javascript Variables

Here are some rules for naming variables : 

1. We should start our variable name with a letter, an underscore _, or the $ sign. For example,

let x = 5; // valid
let _x = 4; // valid
let $x = 3; // valid

2. Variable names cannot start with a number. For example,

let 1x = 5; // invalid

3. Javascript is a case-sensitive language, So x and X is different. For example,

let x = 5;
let X = 4;
console.log(x); // 5
console.log(X);// 4

4. We cannot use Javascript keywords as variable names. For example,

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

Notes:

It is recommended to give descriptive names to your variables. For example, if you are creating a variable to store the number of students, then it’s better to name it studentCount or numberOfStudent rather than x or y.

You can use naming conventions like cameCase to name your variables. In cameCase naming convention we use single or compound words. In compound words, every word will start with a capital letter except the first word which will start with a small letter.

What are Javascript Constants

JavaScript constants are variables whose values ​​cannot be changed throughout the program. We can create a constant with the help of the const keyword which was introduced in ES6(ES2015).

Once a constant is initialized its value cannot be changed.

const x = 5;
x = 4; // Error! Constant can’t be changed.
console.log(x);

You cannot declare a constant without initializing it.

const x; // Error! cannot declare constant without initializing it
x = 5; 
console.log(x);

Note: It is recommended to use const if you are sure that the value of your variable will not change throughout the program. However, some browsers don’t support const. Visit JavaScript const browser support to learn more.

Leave a Comment

Your email address will not be published. Required fields are marked *