Sitemap
Jagjeet Singh

Jagjeet Singh

ES6 JavaScript

3 min readMay 27, 2022

ES6 stands for ECMAScript 2015. Javascript introduce es6 for better code quality or we can set our standard in code. Here ES6 has introduces these points:

Let keyword : let is keyword to create a block level local scoped variable. Local scope means let keyword find inclosing block.
For example :
function Test(x) {

if(x == 1) {

let y = x;

}

console.log(y); // Output : We will get error because y variable can’t be assessable outside of “if” block. {} means block.
}

Const keyword: Const keyword helps to create fixed variable that we can’t change during run-time.
For example : const array = [“User1”, “User2”];
array = [“User3”]; //This is can’t be work because If we have assigned a value to const keyword then we can’t change. We can push some items into array variable
array.push(“User3”); //You will get new array [“User1”, “User2”, “User3”];

for… of Loop: allows you to iterate over iterable objects (arrays, sets, maps, strings etc). It is introduce later version of es6.

for (element of iterable) {
//Every element in the iterable, run the body of the loop.
}

Set : Set is similar to array that store multiple items such as numbers, object, string that can’t contain duplicates values. In simple language we can say that Set remove duplicate values in array.
For example:
var test = [“Test”, “Test2”, “Test”];
new Set(test); //That’s return set object to remove duplicate if we have to return array then
[new Set(test)];

Arrow function : It is allow us to create function in a clear way if we compared to regular function. We are. creating a variable and assigning the function.
For example:
// Regular function
let x = function(x, y) {
return x * y;
}
// using arrow functions
let x = (x, y) => x * y; //We have create a variable with name x and we have assigned the function to return multiplication of two variable
x(2,3); //Output 6

Get Jagjeet Singh’s stories in your inbox

Join Medium for free to get updates from this writer.

Classes : In JavaScript, Classes are help to create object similar constructor.
For example:

// creating a class
class Test {
constructor(name) {
this.name = name;
}
}
new Person('Jagjeet');
// constructor function
function Test2 () {
this.name = 'Jagjeet',
this.age = 26
}
const user = new Test2();// create an object
console.log(user.name)

Module : Import and Export

We can create multiple function in a single file and we want to use that function another file based on our requirement for we need to export so that function accessible in other file with help of import

For example:
// We have file validation.js
function isNotEmpty(value) {

if(!value) return false;

else return true;
}
export
default isNotEmpty;
// another file
import isNotEmpty from './validation.js';

Promises : It will help you to perform asynchronous operation in javascript. Promise has three states
- Pending
- Fulfilled
- Rejected

// returns a promise
let testPromise = new Promise(function (resolve, reject) {
reject('Promise rejected');
});

// executes when promise is resolved successfully
testPromise.then(
function successValue(result) {
console.log(result);
},
)
// executes if there is an error
.catch(
function errorValue(result) {
console.log(result);
}
);

//Output
Promise rejected

Rest parameter and Spread operator :
Rest operator is use to argument of an array or function.
Spread operator is use extend one array to another array.
Example of rest:
function rest(a, b,…argu) {

return [a,b,…argu];
}
rest(1,2,4,5,6);
Output =>[1,2,4,5,6];
Example of spread :
var a = [1,2,3];
var b = […a, 4, 5];
console.log(b); //output [1,2,3,4,5];

Map : It is basically holds key-value pairs of object and it is help to remember the actual insertion order of keys in array / object.

--

--