Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6

A Variable is a place in memory used to store a value. There are some rules for declaration of the variable-

Variable Initialization

Initialization is a process of locating and storing the initial value in the variable. We can assign the value to the variable at its declaration time or before the declaration.

In ECMAScript 6, the var keyword helps usto declare the variable. Here is an example of declaring the variable in ECMAScript 5.

Valid Syntax for Variable Declaration

Here, some examples are given below to show the valid syntax for variable declaration using var keyword-

In ECMAScript 6, we can declare the variables by using the following keywords-

  • The Const
  • The Let

The Const

The const keyword helps us to declare a constant variable. The const keyword is also used as let keyword, but the const keyword builds the blocked-scope variables. The values of the blocked-scope variable cannot be assigned.

There are some points of the Const which are given following-

  • We cannot re-declare the Constant variables.
  •  The Constant variable is immutable.
  • We cannot reassign the value of a constant variable.
  • The Constant variable should only be initialized at the declaration time.

For Example

In the above-given code, a is a constant variable. We assign values (10 and 20) to variable a. An error occurred here because constant variables are immutable; we cannot restore the value in it.

The Let

The let keyword helps us to declare the variables that are declared in the block scope. The Block scope is an area where we declare the let variable.

Difference between let and var keyword

The let and var keywords are used to declare a variable. The main difference between let and var is that the var keyword is a function scoped variable, and the let keyword is a block-scoped variable.

Example

Here, we will discuss an example of a variable declaration using let and var.

By using the let keyword

Output

The output of the above given-example is a syntax error because we cannot reassign the value to the variable ‘a.’

By using the var keyword

The above code is executed, and we got the following output.

Variable Scope in JavaScript

The term scope refers to the visibility of the variable. It means which part of our source code it uses. In other words, the variable scope is the current context of the source code.

There are two types of the variable scope defined in JavaScript.

  • Global Variable
  • Local Variable

Global Variable

If the variable has a global scope, then the developer can easily access the variable from each part of the program. If we declared a variable outside the function, then it becomes a global variable.

For Example

We will understand the global variable with an example given below-

After successful execution of the code, we got the following output-

Output

Local Variable

If the variable has a local scope, then the developer can access it within the specific function where the variable is declared. It means the developer cannot access the local variable from anywhere, like a global variable.

For Example

We will understand the local variable with an example given below-

After execution of the code, we got the following output-

Output

Dynamic Typing in JavaScript

The term 'Dynamic type' can be defined as that language in which the interpreter allot the type to the variable at run time, according to the stored value. The JavaScript provides a dynamic typing feature. It means there is no need to define the type of value stored in a variable.

If the type of the value stored in the variable has been modified during the execution of code, then the dynamic typing starts automatically. Similar to JavaScript, many languages support dynamic typing. For example, Ruby, Pearl, and Python, etc.

Variable hoisting and ECMAScript 6

The term Hoisting can be defined as the default nature of any programming language that is used to shift all types of the declaration to the top of function, script, and scope. The variable hoisting provides us the facility to use the variable before the declaration. JavaScript programming language only supports the hoisting of variable declaration, not variable initialization.

For Example

We will try to understand it better with the help of an example-

The execution of the source code will be successful and does not return the declaration error because of the variable hoisting.

If we want to execute the same code in the compiling phase, then the output will be-

In compiling phase

In the compiling phase, the hoisting feature moves the declared variable at the top of the program.

Output

In Variable Initialization, Hoisting is not supportable 

We can perform variable hoisting only on the declaration of the variable, not on the variable initialization.

Variable initialization before using

In Compiling Phase

Output

We will get the following output after the execution of the code.

Variable initialization after using

In Compiling Phase

Output

We will get the following output after the execution of the code.


Related Topics

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.

Environment Set up in ES6

Environment Set up in ES6 JavaScript is a portable language. It can execute on any browser or any host, and any operating system. Before the use of JavaScript, we need to...

5 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Boolean in ES6

Boolean in ES6 In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a...

4 minutes read.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Types of Inheritance in ES6

Types of Inheritance The term inheritance can be divided into following three parts- Single-level InheritanceMultiple InheritanceMulti-level Inheritance Single-level Inheritance The single-level inheritance can be defined as an inheritance in which the child class can...

5 minutes read.

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 minutes read.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.