ES7 and ES8 features
String.prototype.padStart
/padEnd
padStart
and padEnd
allow us to pad a given string with any text of our choice to ensure a string matches a given length:
// padStart(desiredLength, textToPrepend)Examples:-'abc'.padStart(12, 'Hi') // 'HiHiHiHiHabc'// Only use what gets to length
'9894'.padStart(9, '6363') // '636369894'// padEnd(desiredLength, textToAppend)Examples:-'99'.padEnd(9, '01') // '990101010'''.padEnd(9, '01') // '010101010'
Exponentiation
JavaScript has introduced a shorthand method of exponentiation:
// 2 to the power of 10
Math.pow(2, 10) // 1024// ..becomes
2 ** 10 // 1024
It will reduce the size of the code.
Object.entries
Object.entries
allows us to get an object's enumerable property pairs in an array format ([key, value]):
// Object literal
Object.entries({ 'A': 'a', 'B': 'b' }); // [["A","a"],["B","b"]]// String
Object.entries('VIKAS') // [["0","V"],["1","I"],["2","K"],["3","A"],["4","S"]]
Object.entries
follows the same order as work in for loop etc.
Object.values
Object.values
allow us to convert an object into an array:
// Object literal
Object.values({ 'z': 11, 'y': 17 }) // [11, 17]
//let store in some variable
abc = Object.values({ 'z': 11, 'y': 17 })
Array.isArray(abc) //true// Array-like object (order not preserved)
Object.values({ 90: 'ninety', 990: "abc", 1: 'yes' }) // ['yes','ninety','abc']// String
Object.values('kohli') // ["k", "o", "h", "l", "i"]// Array
Object.values([6, 7, 8, 9, 2, 3]) // [6,7,8,9,2,3]
Object.values
provides value entries in object literals, arrays, strings, etc.
Array.prototype.includes
Array.prototype.includes
is somewhat similar like indexOf
but instead of returning its index, this returns a boolean value (true or false)
['v', 'i', 'k', 'a', 's'].includes('s') // true --> not index
['v', 'i', 'k'].includes('l') // false --> not index
Trailing Commas
let myObj = { ba:'fdfdfdb', db: 'fddfcfd', } // No error in JS as well!let myArr = [1, 2, 3, ] // No error in JS as well![6, 7, 8, 9,].length // 4
[9, 8, 7, , , , , , , , ].length // 10
As in many languages, this feature is already there like Python and many more.
Also, I saw David Walsh post for the same, you can also visit his link