Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

JavaScript Objects and Arrays Useful Methods

JavaScript Objects and Arrays have many useful methods that can help you write clean and maintainable code. In this post we will talk about following methods:

  • Object.keys()
  • Array.prototype.forEach()
  • & Array.prototype.reduce()
  • Array.prototype.push() & Array.prototype.shift()

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 Object.keys() over for ... in is that, it does not look up in the prototypical inheritance chain of the given object.

Syntax : Object.keys(obj)
Input : valid JavaScript Object (either array or JSON)
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 :
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.