Some essential ES6 features

AhmedAmeer
4 min readMay 3, 2021
  1. Variables

The purpose of a variable is to store information (values, expressions) which can be used later. The process of creating a variable is also known as declaring a variable. When you declare a variable, you tell the computer to reserve some memory for your program to store some data. some types of variables in java scripts are let, var and const

Var variable

Declaring a variable using the ‘var’ keyword allows that variable to be used within the function and outside the function.

Example

In this example the variable ‘ i ’ can be access within the function and outside the function

Output of the function

The line number 16 ‘ console.log(“inside the loop : “+i); ’ will be print from 1 to 4

The line number 18 ‘ console.log(“inside the loop : “+i); ’ will be 5 only

Let variable

Declaring a variable using the ‘ let ’ keyword only can accessible within a block you define . and you cannot access outside the block.

Example

In this example the variable ‘ i ’ can be access within the function and we cannot access the ‘ i ’ variable outside the function.

The line number 16 ‘ console.log(“inside the loop : “+i); ’ will be print from 1 to 4

The line number 18 ‘ console.log(“inside the loop : “+i); ’ will be prompt an error called “ Uncaught ReferenceError : i is not defined“ only

Const variable

Const variable is read only we cannot re assign the variable

Example

output

Defining a variable with const set into 1 and if we reassign it 4

It will be displaying an error

2. OBJECTS

Objects in java scripts are collection of key values and pairs. The attributes or functions could be accessed by using the dot operator as shown in the image below.

3. Arrow Function

Arrow function is function which reduce the code of the normal function

Rules

1.if there is only one parameter then no need a paranthese bracket() to the arrow function.

2.if there is no parameter then use only simple bracket().

3.if there is only one line of return statement then no need a return key word and no need a curly brackets{}.

Example1

Converting the normal function into arrow function

  1. adding two numbers

2.calculating square number

4 . Spreading the arrays(combing two arrays)

The concat method can be used to combine two arrays together. However if there are more than two arrays to combine or elements to add then this becomes a bit complicated but in ES6 the spread operator could simply combine as many arrays or even elements as desired. As the name implies the spread operator spreads out all the elements in contains.

Example 1

Spreading 2 arrays(concat method )

Example 2

Spreading 3 arrays

Example 3

--

--