Member-only story
5 Cool Modern JavaScript Features Most Developers Don’t Know
Write less and do more with JavaScript
With JavaScript, you can do one thing in many different ways. Also, JavaScript is evolving with the release of every new ECMAScript specification, adding new useful methods and operators to make the code shorter, and somewhere more readable.
After offering ten JavaScript tricks, I’ve decided to share five more tricks that you can do with JavaScript.
1. Object.entries
Most developers use Object.keys
method to iterate over an object. This method only returns an array of the object keys, not values. We can use Object.entries
to get both key and value.
const person = {
name: 'John',
age: 20
};Object.keys(person); // ['name', 'age']
Object.entries(data); // [['name', 'John'], ['age', 20]]
And to iterate over an object we can do the following:
Object.keys(person).forEach((key) => {
console.log(`${key} is ${person[key]}`);
});// using entries to get key and value both
Object.entries(person).forEach(([key, value]) => {
console.log(`${key} is ${value}`);
});// expected output:
// name is John
// age is 20