JavaScript Variables

Basically we can declare variables in three different ways by using var, let and const keyword. Each keyword is used in some specific conditions.

Declare a variable

To declare a variable, you use the var keyword followed by the variable name as follows:

var name;

A variable name can be any valid identifier. By default, the name variable has a special value undefined if you have not assigned a value to it.

Variable names follow these rules :

  • Variable names are case-sensitive. This means that the name and Name are different variables.
  • Variable names can only contain letters, numbers, underscores, or dollar signs and cannot contain spaces. Also, variable names must begin with a letter, an underscore (_) or a dollar sign ($).
  • Variable names cannot use the reserved words.

By convention, variable names use camelcase like nameyourAge, and lastName.

JavaScript is a dynamically typed language. This means that you don’t need to specify the variable’s type in the declaration like other static typed languages such as Java or C#.

Starting in ES6, you can use the let keyword to declare a variable like this:

let name;

Initialize a variable

Once you have declared a variable, you can initialize it with a value. To initialize a variable, you specify the variable name, followed by an equals sign (=) and a value.

let name;
name = "lion";

To declare and initialize a variable at the same time, you use the following syntax:

let variableName = value;

For example, the following statement declares the name variable and initializes it with the literal string "lion":

let name = "lion";

JavaScript allows you to declare two or more variables using a single statement. To separate two variable declarations, you use a comma (,) like this:

let name = "lion",
   place = "forest" ;

Change a variable

let name = "lion";
    name = "Tiger" ;

Once you initialize a variable, you can change its value by assigning a different value. For example:

Undefined vs. Undeclared variables

The difference between undefined and undeclared variables in JavaScript is: Undefined variable means a variable has been declared but it does not have a value. Undeclared variable means that the variable does not exist in the program at all.

Undefined Example :

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

In this example, the name variable is declared but not initialized. Therefore, the name variable is undefined.

Undeclared variables :

console.log(place);  

Output :


console.log(place); 
            ^
ReferenceError: place is not defined

In this example, the place variable has not been declared. Hence, accessing it causes a ReferenceError.

Constants

The const keyword was introduced in ES6 (2015).

Variables defined with const cannot be Redeclared.

Variables defined with const cannot be Reassigned.

Variables defined with const have Block Scope.

JavaScript const variables must be assigned a value when they are declared:

const workday = 5;

I hope you like this article.

Also Read :