JS Variables

JavaScript variables are containers for storing data values.

In this example, xy, and z, are variables:

var x = 5;
var y = 6;
var z = x + y;

Output – It will assign and calculate value as given below:

  • x stores the value 5
  • y stores the value 6
  • z stores the value 11

JavaScript variables are containers for storing data values.

eg. Declaring a variable (container1, container2, container3) and assigning a quality value to each.

A variable

variable is a “named storage” for data. We can use variables to store goodies, visitors, and other data.

JavaScript ES6 var-let-const - Launch School - Medium
JavaScript ES6

In this article, varlet and const will be given with respect to their scope, use and hoisting. Let’s make a note of the differences between them.

1. VAR

Before the starting of ES6, var declarations ruled as King. There are some uncertainty associated with variables declared with var though.

First though, let us get to understand var more before we discuss one of such issues.

Scope of var

Scope essentially means where these variables are available for use. It is globally scoped when a var variable is declared outside a function.

This means that any variable that is declared with var outside a function

block is used as global scope. var is function scoped when it is declared within a function.


To understand further, look at the example below.

Global Scope

Variables declared Globally (outside any function) have Global Scope.

// Global Scope using var

var Name = "Sagar Kalyan";

// code here can use Name

function myFunction() {
  
// code here can also use Name

}

JavaScript programming allows global variables to be used from anywhere placed inside the coding.

Function Scope

Variables declared Locally (inside a function) have Function Scope.

// FUNCTION SCOPE using var
// code here can NOT use Name

function myFunction() {

  var Name = "Sagar Kalyan";

  // code here CAN use Name

}

// code here can NOT use Name

Local variables can only be accessed from within inside the function as they are declared.

Re-declaring Variables

Redeclaring a variable using the var keyword can impose problems.

Re-declaring a variable inside a block will also redeclare the variable outside the block:

var a = 11;
// Here a is 11
{
  var a = 5;
  // Here a is 5
}
// Here a is 5

2. The “Let” Keyword

Redeclaring a variable using the let keyword can solve this problem.

Re-declaring a variable inside a block will not re-declare the variable outside the block:

var a = 11;
// Here a is 11
{
  let a = 5;
  // Here a is 5
}
// Here a is 11

3. The “const” keyword

If a variable is defined using the const keyword, its scope is limited to the block scope. The variable cannot be re-assigned to a diff. value.

{
 const PI=3.14;
 console.log(PI);
} //block 1


{
  console.log(PI);
} //block 2


/* As we are using "const PI=3.14", scope of "PI" is limited to block 1 and "PI" is not recognized in block 2 */

Important Note – Read this:

The differences in var, let, const , here they are :

  1. var declarations are globally scoped or function scoped while let and const are block scoped.
  2. variables var can be updated, and re-declared and used within its scope; variable let can be updated, but not re-declared; variable const can neither be updated nor re-declared.
  3. They are all hoisted to the top of their scope but while varvariables are initialized with undefinedlet and const variables are not initialized.
  4. While var and let can be declared without being initialized, const must be initialized during declaration.

Leave a Reply

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