ES6 new features – Part 2

Before diving into the core concepts like ‘Class’ and ‘Promises’ of ES6, let us see some minor changes which really helps in daily life coding.

Destructuring Assignment

Destructuring assignment on arrays allows for much more efficient array manipulation and assigns multiple variables from array data based on the index value.

Destructuring assignment on objects allows for more concise object manipulation and assigns multiple variables from object data based on the key names.

Spread Operator

The spread operator spreads individual values to allow for expansion with multiple arguments and elements. Denoted by three periods ‘…’

Helper methods

1.Array helper methods
map
map is used to traverse an array and return an array by iterating over each element of an initial array.

filter
The filter method iterates over an array and returns an array of elements which pass the test specified.

reduce
The reduce method applies a function against an accumulator(initial value for the first time and the returned value from next) and each element in the array (from left to right) to reduce it to a single value.
Syntax
var result = array.reduce(callback[,initialValue]);
callback: function to execute each element in the array, which accepts accumulator,current value, current index and the array.
initialValue: value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used.
If the array is empty and no initialValue is provided, TypeError will be thrown. If the array has only one element (regardless of position) and no initialValue is provided, or if initialValue is provided but the array is empty, the solo value will be returned without calling callback.

2.String helper methods
repeat
Returns a string which is the concatenation of the first string to itself by the number of times specified.

startsWith
This checks if the specified parameter string is at the beginning of the string which is getting tested. Returns true if the string starts with the parameter string, false otherwise.

endsWith
This checks if the specified parameter string is at the end of the string which is getting tested. Returns true if the string ends with the parameter string, false otherwise.

includes
Returns true if the passed string exists in the string which is getting tested, false otherwise.

3.Number helper methods
‘Number’ is the global object in javascript like math.

isFinite
Returns a boolean by checking if the passed parameter is a finite number or not.

isSafeInteger
Returns a boolean if the passed element satisfies the integer conditions.

Difference between let and var keyword

‘let’ keyword helps preserve block scoping whereas ‘var’ keyword keeps the scope locally or globally which is also called as lexical scoping or function scoping.




Tagged as: ,