Variables and Constants in JavaScript

Variables and Constants in JavaScript

In this article, we will learn about Variables and Constants in JavaScript.

JavaScript Variables

A variable is a container (storage area) for data in programming. For example:

let x = 3;

Here, x is a variable and It’s storing 3.

Declation of Variables

To declare variables in JavaScript, we use either var or let keyword. For example:

let x;
var y;

Here, x and y are variables.

Difference between var and let

To declare variables, both var and let are used. however, they do differ in some ways.

varlet
● var is the old way of declaring variables in JavaScript versions prior to ES6.
● var is a function scoped variable (will be discussed in later tutorials).
● For instance, var x;
● let is the new way of declaring variables starting with ES6 (ES2015).
● let is a block scoped function (will be discussed in later tutorials).
● For instance, var y;

Note: It is suggested that we use let instead of var. However, some browsers do not support the let variable.

Intialization of Variables

To assign a value to a variable, we use the assignment operator ‘=’.

let x;
x = 3;

Here, 3 is intialized to variable x.

Variables can also be initialised during the declaration process.

let x = 3;
let y = 5;

Variables can be declared in a single statement in JavaScript.

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

If you use a variable without first initialising it, the value will be undefined.

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

Here, x is the variable, and it is undefined because it has no value.

Change the Value of Variables

It is possible to alter the value of the variable. For example:

// 3 is assigned to variable x
let x = 3;
console.log(x); // 5
// vaue of variable x is changed
x = 7;
console.log(x); // 7

Rules for Naming JavaScript Variables

Rules of naming a JavaScript variable are:

  • The name of a variable must begin with a letter, an underscore _, or the dollar sign $. For example:
let x = ‘Developerdome’;
let _x = ‘Developerdome’;
let $x = ‘Developerdome’;
  • The names of variables cannot begin with a number. For example:
let 1x = ‘Developerdome’;
  • JavaScript is a case-sensitive language. So y and Y are different variables. For example:
let x = ‘Developerdome’;
let X = 3;

console.log(x); // Developersdome
console.log(X); // 3
  • Keywords are not permitted to be used as variable names. For example:
let new = ‘Developerdome’; // Error!! new is a keyword.

Note: Though we can name variables in any way we want, but it’s a good practice to give a descriptive variable name. For example, If we are using a variable to store the number of bananas, it better to use bananas or numberOfBananas rather than x or y.
If a variable has multiple words, it is usually written in camelCase in JavaScript. For example, firstName, annualSalary, etc.

JavaScript Constants

In the ES6(ES2015) version, the const keyword was added to create constants in JavaScript. For example:

const x = 3;

We can’t change the value of a constant once it’s been initialised.

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

In simplest way, a constant is a type of variable whose value cannot be changed.

And also, we cannot declare a constant without initializing it. For example:

const x ; // Error!! Missing intializer in const declaaration.
x = 5;
console.log(x);

Note: Const is recommended if you are sure that the value of a variable will not change throughout the program. However, there are some browsers that do not support const.

That’s all about the Variables and Constants in JavaScript.

You may like Introduction to JavaScript | Web Development.

Hope this article will guide you to recognize about the Variables and Constants in JavaScript that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has One Comment

Leave a Reply