JavaScript A to Z Tutorial : Variables

javascript tutorial variables

JavaScript Variables are a type of container that after being declared, the values of the data are stored in it. For example, your mother keeps papad, pickle, rice, etc. in a tin box in your house. Similarly, different types of data are also stored in Variables, so that we can use them in the program if needed.

How to Declare Variables in JavaScript?

We have three ways to declare Variables in JavaScript.

By Using “var” keyword

var a=5;

By Using “const” keyword

When the values of the variable do not have to be changed then const is useful.

const pi=3.14;

By Using “let” keyword

let a=5;

In the above three methods, the value 5 gets assigned to the variable a. The process of assigning the value of a variable is called Variable Initialization.

You can also declare any variable without using var, let, const keywords.

myVar = 5;

But the above three methods are more recommended for variable declaration.

The use of var for variable declaration is out of date, so a modern JavaScript programmer should use const and let. This is essential for memory management.

Rules for Declaring Variables

Some standard rules are followed in JavaScript to create a variable, which is as follows. To declare variables, a unique name has to be given which is called “Identifiers”.

  • Variable names can never use the names of reserved keywords. like- function, var, const, document, let, etc. See the list of JavaScript Reserved Keywords.
  • Variable names are case sensitive, myVar and MyVar are two different variables.
  • JavaScript treats $ and _ as a letter so identifier names can start with $ and _.
Neeraj Yadav
Howdy Friends! I am Neeraj Yadav a professional Web and App developer. I am trying to share my knowledge through this website. Here you will get amazing coding projects, free courses, programming articles.