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.
// // Declaring an Array var myArray = new Array(); // Using Array Constructor, try to avoid this var myArray = Array(); // not preferred var myArray = []; // Using Array literal, always use this. // Declaring and initialising var myArray = new Array(element1,element2,element3); var myArray = Array(element1,element2,element3); var myArray = [element1,element2,element3]; // preferred array var anotherArray=[3,4,"first String",9,{element1:"another string"}] // array with mixed type element var myArray = new Array(10,20) // create an array with 2 elements var myArray = new Array(20) // create an array with 20 undefined elements var myArray = Array(10,20) // create an array with 2 elements var myArray = Array(20) // create an array with 20 undefined elements var myArray = []; myArray.Length = 20 // create an array with 20 undefined elements //
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.
// var array1 = [1,2,3]; var array2 = [4,5,6]; var array3 = [7,8,9]; console.log(array1) // output [1,2,3] // joining multiple array array1 = array1.concat(array2,array3); console.log(array1) // output [1,2,3,4,5,6,7,8,9] // adding array elements with an existing array array1 = array1.concat(10,11); console.log(array1) // output [1,2,3,4,5,6,7,8,9,10,11] //
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.
// var array1 = ["apple","banana"]; console.log(array1.join()); // will print apple,banana console.log(array1.join("-")); // will print apple-banana //
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 “,”.
// var array1 = ["apple","banana"]; console.log(array1.toString()); // will print apple,banana console.log(array1.toString("-")); // will print print apple,banana //
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.
// var array1 = [1,2]; array1.push(3); // will add 3 at the end of the array console.log(array1); // will print [1,2,3] //
pop()
Pop method removes the last element from an array and return that element.
// var array1 = [1,2,3]; var last = array1.pop(); // remove the last element from an array console.log(last); //will print 3 console.log(array1); // will print [1,2] //
shift()
Shift() method removes the first element from an array and returns that element.
// var array1 = [1,2,3]; var removedElement = array1.shift(); // will add 3 at the end of the array console.log(array1); // will print 2,3] console.log(removedElement);// will print 1 //
unshift()
unshift() method adds new elements at the beginning of the array.
// var array1 = [1,2,3]; array1.unshift(4,5); console.log(array1); // will print [4,5,1,2,3] //
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.
// //