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 use­ful meth­ods that can help you write clean and main­tain­able code. In this post we will talk about fol­low­ing methods:

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

We will first see a few quick exam­ples of each of the above meth­ods and even­tu­ally we will solve a real world interview-style prob­lem using these methods.


It returns an array of enu­mer­able prop­er­ties of an object. The advan­tage of using Object.keys() over for ... in is that, it does not look up in the pro­to­typ­i­cal inher­i­tance chain of the given object.

Syn­tax : Object.keys(obj)
Input : valid JavaScript Object (either array or JSON)
Out­put : an array


This meth­ods calls the call­back for each valid ele­ment in the array. The call­back has the access to the index vari­able which can be used to access the index of the ele­ment in the array.

Syn­tax : arr.forEach(callback)

It calls the call­back on each valid ele­ment of an array and returns a new result­ing array.

Syn­tax :
Out­put : array


It calls the call­back on each valid ele­ment of an array and returns a sin­gle result­ing value.

Syn­tax : arr.reduce(callback)
Out­put : sin­gle value


It returns the length of the new array and it adds the ele­ment at the end of the array.

Syn­tax : arr.push(e1 ‚…, eN)
Out­put : array


It returns the first removed ele­ment from an array and it removes the first ele­ment from the orig­i­nal array.

Syn­tax : arr.shift()
Out­put : first removed ele­ment from an array

The exam­ple shown below uses all the meth­ods we just talked about and solves a real world prob­lem using these methods.

PS: I like vis­it­ing my old code and refac­tor­ing it with some of the new meth­ods intro­duced in the lan­guage. And then includ­ing those lan­guage fea­tures in day to day pro­gram­ming. This exam­ple is one demon­stra­tion of it.