JavaScript ES6 Amazing Topics

Var Declarations:

Example:

var a = 10;

if (a === 10) {

var a = 20;

console.log(a);

// expected output: 20

}

console.log(a);

// expected output: 20

Let Declarations:

Example:

let b = 10;

if (b === 10) {

let b = 20;

console.log(b);

// expected output: 20

}

console.log(b);

// expected output: 10

Constant Declarations:

Example:

const num = 420;

try {

num = 9999;

} catch (error) {

console.log(error);

// expected output: Error: ‘Assignment to constant variable.’

// Note — error messages will vary depending on browser

}

console.log(num);

// expected output: 420

Default function parameters:

Example:

function multiply(a, b = 10) {

return a * b;

}

console.log(multiply(9, 8));

// expected output: 72

console.log(multiply(43));

// expected output: 430

JavaScript Anonymous Functions:

Example:

let showMessage = function () {

console.log(‘This is Anonymous function’);

};

showMessage();

// expected output: This is Anonymous function

Spread Operator:

Example:

let colors = [‘Red’, ‘Yellow’];

let newColors = […colors, ‘Violet’, ‘Orange’, ‘Green’];

console.log(newColors);

// expected output: [ ‘Red’, ‘Yellow’, ‘Violet’, ‘Orange’, ‘Green’ ]

Arrow function:

Arrow functions allow us to write shorter function syntax.

Example:

Normal Function:

text= function() {

return “Happy Coding!”;

}

Arrow Function:

text = () => “Happy Coding!”;

JavaScript Single-line Comment:

Example:

//Normal Function

text= function() {

return “Happy Coding!”;

}

JavaScript Multi-line Comments:

Example:

/*

This is

the example

of normal function

*/

text= function() {

return “Happy Coding!”;

}

Try…Catch:

The try…catch statement has two main blocks: try, and then catch.

const num = 420;
try {
num = 9999;
} catch (error) {
console.log(error);
// expected output: Error: ‘Assignment to constant variable.’
// Note — error messages will vary depending on browser
}
console.log(num);
// expected output: 420

Web Developer