Signup/Sign In

JavaScript Variables - var, let and const

A variable in JavaScript is a named memory space that is used to store a value. A variable is like a container in which you can store any data or value.

A variable in JavaScript, has a name, a value, and a memory address. (You don't have to worry about the memory address, just focus on the name and value)

  • The name of the variable uniquely identifies the variable,

  • The value refers to data stored in the variable, and

  • The memory address refers to the memory location of the variable, where the value is stored.

You need to declare or create a variable before using it.

In JavaScript, the var, let, and const keywords are used to declare a variable. Initially there was only var keyword, but, starting from ES6, now we can also declare variables using the let keyword.

Rules for Variable Name in JS

The following are some rules to keep in mind while declaring variables.

  • Variable names cannot contain spaces.
  • The first letter of the variable can be [a-z, A-Z], a dollar sign ($) or an underscore (_), after the first letter of the name any digit [0-9] can be used.

  • Variable names are case-sensitive. For example: let a and let A both are different.

  • We can use either var or let keyword to define variables and the const keyword to define variables with constant value.

  • We can not use reserved keywords as the name of the variables in JavaScript, like for, class, etc.

Syntax for Creating a Variable in JS

Following is the syntax for declaring or creating a variable and assigning values to it.

var variable_name;
// or
let variable_name;

When we have to define multiple variables, we can do it like this:

// declaring 3 variables together
var x, y, z;

As we have already learned that JavaScript is Dynamically typed, so we do not have to worry about specifying the data type of the value that we will store in the variable. JavaScript automatically detects that.

JavaScript Variable Example:

Now let's take a simple example where we will declare a variable and then assign it a value.

let employeeName;   // Declaring a variable

let employeeName = "Rahul";   // Declaring and assigning at the same time

You can initialize or assign a value to the variable at the time you declare the variable or you can just declare the variable and initialize the variable later on.

If you just create or declare a variable then the value stored in it is undefined unless you assign it a value.

Create Variable using let

Using the let keyword, you can create a local variable in JavaScript. But what is a local variable? Well, a local variable is a variable that ceases to exist after the scope in which it is defined, ends. A scope is created using curly brackets {}.

Let's see an example,

In the code example above, we have created a simple block of code using the curly brackets {}. The output of the above code will be a ReferenceError.

If you access the variable inside the block scope, or without any scope, there will be no error. For example,

let msg;
msg = "Welcome to Studytonight";
console.log(msg)

When you create a variable using the let keyword then that variable is only available inside the scope in which it is defined, not outside it.

You should always create a variable using the let keyword.

Create Variable using var

Using the var keyword, you can create a variable with a global scope. If you create a variable using the var keyword, then that variable is available everywhere in that JavaScript code file.

You may define the variable with var keyword inside a scope, that variable is available even after the scope ends.

Let's see an example,

Don't get confused by the block of code created, it is just to represent a scope created when you define a function, or use a loop, that you will learn in upcoming tutorials.

With or without a scope defined, the variables created using the var keyword have a global scope.

Create Variable using const

You can also create a variable using the const keyword. Variables created using the const keyword will have a constant value stored in them, and that value can never be changed or updated.

When you create a variable using the const keyword, then you have to create the variable and assign it a value while creating the variable. If you don't assign a value to the variable then JS will give an error.

const msg;
msg = "Welcome to Studytonight";


const msg;
^^^

SyntaxError: Missing initializer in const declaration

Hence, the correct syntax for creating a new variable using the const keyword is,

const msg = "Welcome to Studytonight";

You have to create the variable and assign it the value in the same line of code.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight