Rest and Spread Operators in ES6

ES6 added two important new operators: Rest and Spread. Technically they look the same ( ... => three dots) but they are used in different places. Rest: function sumUp(start, ...toAdd) {} Transforms a list of arguments (1, 2, 3) into an array ([1, 2, 3]) which may be used inside the function. This behavior is triggered when used inside of a function argument list. Spread: let ids = [1, 2, 3, 4, 5, 6]; console....

December 25, 2022 · 1 min · Daman Arora

Arrow Functions and Default Arguments in ES6

In ES6 there were two major additions regarding functions: (Fat) Arrow Functions( () => {} ) and default arguments( doSmth(arg = 1) ). Fat Arrow Functions allow you to use a shorter syntax to create functions: const fn = (arg1, arg2) => return arg1 + arg2 You may leave out the parenthesis around the arguments if only one argument is passed. If no argument is passed, empty parentheses are required. The function body may be written inline and without curly braces if you’re only writing a return statement (return then also may be left out)....

December 25, 2022 · 1 min · Daman Arora

let and const keywords in ES6

let and const are new keywords introduced in ES6 which enable us to change the way we work with variables and constants. Let and const are both block scoped. Prior to ES6, we had only function scope and global scope. Block scope restricts variable access to blocks, i.e. constructs with curly braces, such as if...else, for loops, etc. As an example: In the past, we would declare variables using var as follows:...

December 25, 2022 · 2 min · Daman Arora