Mastering JavaScript Part 1, JavaScript Cheat Sheet 1

In this particular post I will write all the comments in inside the code. It will be easy to have all the functions in a single location so that you can go through all the topics all at once.

Methods of Array

Array has a number of methods that help us to do operation in an array easily.

concat() method

concat() joins two or more arrays and returns a new array. Instead of joining two or multiple arrays you can even add a new elements with a single array.

Converting an array to a string, join() and toString() method

If you want to convert an array to a string there are two predefined methods.

join() method

Join() method you can specify a separator.If you do not use any separator than it will use comma “,” as default separator.

toString() method

You can convert an array to comma separated string by using toString() method. You can not change your separator in toString() method. It will be always a comma “,”.

Adding or removing elements from an array push(), pop(), shift(), unshift()

You can add a new element or remove an existing element from an array in a number of ways. Based upon your logic you can use any of them.

push()

Push method add an element at the end of the array.

pop()

Pop method removes the last element from an array and return that element.

shift()

Shift() method removes the first element from an array and returns that element.

unshift()

unshift() method adds new elements at the beginning of the array.

In short, if you want to add new elements with an array you can use either push() or unshift() methods.
On the other hand if you want to remove elements from an array then you can use either pop() or shift() method.

  • push() method add new elements at the end of the array.
  • unshift() method add new elements at the beginning of the array.
  • pop() method remove an element from the end of the array and returns that element.
  • shift() method remove an element from the beginning of the array.