ECMAScript 8 or ECMAScript 2017 was officially released last year end of June by TC39 (what is TC39? Technical Committee 39 which is the official committee for the evolution of Javascript. This committee conducts meeting regularly and usually the members are browser vendors ). Now the standard is to publish new specifications once a year. ES5 was published in 2009 and after that, the major release was ES6 in 2015, ES7 in 2016.
String.prototype.padStart
/padEnd
padStart
and padEnd
allow us to pad a given string with any text of our choosing to ensure a string matches a given length:
// padStart(desiredLength, textToPrepend)
// No text
''.padStart(10, 'Hi') // 'HiHiHiHiHi'
// Some text
'def'.padStart(6, 'abc') // 'abcdef'
// Only use what gets to length
'5678'.padStart(7, '1234') // '1235678'
// padEnd(desiredLength, textToAppend)
'23'.padEnd(8, '0') // '23000000'
One usage of padStart
could include prepending an area code to phone number if the user input isn't the correct length. padEnd
could be used for decimal precision.
Object.entries
Object.entries
allows us to get an object's enumerable property pairs in array format ([key, value]):
// Object literal
Object.entries({ 'a': 'A', 'b': 'B' }); // [["a","A"],["b","B"]]
// String
Object.entries('david') // [["0","d"],["1","a"],["2","v"],["3","i"],["4","d"]]
Object.entries
follows the same order as for...in
would.
Object.values
Object.keys
has been immensely useful for me so I was also excited to see Object.values
introduced:
// Object literal
Object.values({ 'a': 23, 'b': 19 }) // [23, 19]
// Array-like object (order not preserved)
Object.values({ 80: 'eighty', 0: 1, 1: 'yes' }) // [1, 'yes', 'eighty']
// String
Object.values('davidwalsh') // ["d", "a", "v", "i", "d", "w", "a", "l", "s", "h"]
// Array
Object.values([1, 2, 3]) // [1, 2, 3]
Object.values
provides value entries in object literals, arrays, strings, etc.
Array.prototype.includes
Array.prototype.includes
is a bit like indexOf
but instead returns a true
or false
value instead of the item's index:
['a', 'b', 'c'].includes('a') // true, not 0 like indexOf would give
['a', 'b', 'c'].includes('d') // false
indexOf
has been used over the years to detect item presence in array, but the `0` index can lead to false negatives if not coded properly. I'm glad JavaScript has added a function that returns exactly what we need: a positive or negative answer!
Exponentiation
JavaScript has introduced a shorthand method of exponentiation:
// 2 to the power of 8
Math.pow(2, 8) // 256
// ..becomes
2 ** 8 // 256
This new syntax accomplishes the same as Math.pow
with less code!
Trailing Commas
I'm old enough to remember the days where a trailing comma would completely explode your JavaScript code in Internet Explorer 6. JavaScript now accommodates for the extra comma:
let myObj = { a:'b', b: 'c', } // No error!
let myArr = [1, 2, 3, ] // No error!
[1, 2, 3,].length // 3
[1, 2, 3, , , ].length // 5
The case of the Array length is one to keep in mind. ESLint has a comma-dangle
rule you can use to ensure your comma dangle usage is consistent.
ES9 is on the way hope it will meet all the wishlists of the developer!!
Thanks for Reading!!