We will first see a few quick examples of each of the above methods and eventually we will solve a real world interview-style problem using these methods.
It returns an array of enumerable properties of an object. The advantage of using
for ... in is that, it does not look up in the prototypical inheritance chain of the given object.
Syntax : Object.keys(obj)
Output : an array
This methods calls the callback for each valid element in the array. The callback has the access to the index variable which can be used to access the index of the element in the array.
Syntax : arr.forEach(callback)
It calls the callback on each valid element of an array and returns a new resulting array.
Syntax : arr.map(callback)
Output : array
It calls the callback on each valid element of an array and returns a single resulting value.
Syntax : arr.reduce(callback)
Output : single value
It returns the length of the new array and it adds the element at the end of the array.
Syntax : arr.push(e1 ‚…, eN)
Output : array
It returns the first removed element from an array and it removes the first element from the original array.
Syntax : arr.shift()
Output : first removed element from an array
The example shown below uses all the methods we just talked about and solves a real world problem using these methods.
PS: I like visiting my old code and refactoring it with some of the new methods introduced in the language. And then including those language features in day to day programming. This example is one demonstration of it.