Javascript Array Methods – Tutorials & Code Examples. In this tutorial, we are going to learn about javascript array methods with code example.
Summary of topics which we cover in this tutorial.
- What is an Array?
- The Syntax of array declaration in Javascript.
- Javascript array methods.
What is an Array?
Let’s first consider a situation where we have a list of items and we need to store them in a single variable. It could look like this:
1 2 3 4 5 |
var mobile1 = "Samsung"; var mobile2 = "Nokia"; var mobile3 = "Iphone"; |
Let’s assume our list has more than 500 items. Are we going to declare 500 variables? To handle such type of situation every programming language provides a data structure called array.
An array is a variable which holds multiple values of same types. It is an ideal choice for storing the list of values in a single variable.
For example –
1 2 3 |
//Example of an array var mobiles = ["Samsung", "Nokia", "Iphone"]; |
How to Declare an Array in Javascript
There are multiple ways through which we can declare an array in Javascript.
i) Declare an array in Javascript using Array constructor.
1 2 3 4 5 6 7 8 |
//Array declaration using array constructor var firstArray = new Array(); firstArray[0]="PHP"; firstArray[1]="apache"; firstArray[2]="javascript"; console.log(firstArray); |
We can define the above statement in a single line.
1 2 3 |
//Declare an array var firstArray = new Array("PHP","apache","javascript"); console.log(firstArray); |
2. Declare an array in Javascript using literal notation
1 2 3 |
//Array declaration using literal notation var firstArray = ["PHP","apache","javascript"]; console.log(firstArray); |
For simplicity, readability and execution speed array declaration using javascript literal notation is preferred.
Javascript Array Methods
How to check if a variable/object is an Array in Javascript
In javascript, Array.isArray() method is used to check whether a variable/object is an array or not. Array.isArray() method returns true if an object is an array otherwise false.
1 2 3 4 5 6 |
//Examples console.log(Array.isArray(["PHP","Apache","Javascript"])) // Return True console.log(Array.isArray("PHP")); //Return false console.log(Array.isArray([2])); //Return true |
How to get current date and time in javascript
Find Length of an Array in Javascript using Array Length Property
Javascript array length property returns the length of an array.
1 2 3 4 5 |
var mobiles = ["Samsung", "Nokia", "Iphone"]; console.log(mobiles.length); // Return 3 |
Append New Values at the end of an Array – Javascript Push() Method
Javascript array push() method is used to append one or more values at the end of an array.
1 2 3 4 5 6 7 8 9 |
//Array var mobiles = ["Samsung", "Nokia", "Iphone"]; //Push mobiles.push("Redmi"); console.log(mobiles); // ["Samsung", "Nokia", "Iphone", "Redmi"] |
Append Values at the Beginning of an Array using Javascript unshift() Method
Javascript unshift() method is used to append values at the beginning of an array.
1 2 3 4 5 6 7 8 9 |
//Array var firstArray = ["PHP", "apache", "javascript"]; //Append HTML5 firstArray.unshift('HTML5'); console.log(firstArray); //["HTML5", "PHP", "apache", "javascript"] |
Remove Last Element of an Array in Javascript using pop() Method
Javascript pop() method is used to remove last element of an array.
1 2 3 4 5 6 7 |
var tech = ["PHP", "apache", "javascript", "HTML5"]; tech.pop(); //Remove HTML5 (last element of an array) console.log(tech); //["PHP", "apache", "javascript"] |
Remove First Element of an Array in Javascript using shift() Method
Javascript shift() method is used to remove first element of an array.
1 2 3 4 5 6 7 |
var tech = ["PHP", "apache", "javascript", "HTML5"]; tech.shift(); // Remove PHP (first element of an array) console.log(tech); //["apache", "javascript", "HTML5"] |
How to Combine Two or More Arrays in Javascript using concat() Method
Javascript concat() method is used to combine two or more arrays.
1 2 3 4 5 6 7 8 9 10 11 12 |
//First array var firstArray = ["PHP", "apache", "javascript", "HTML5"]; //Second array var secondArray = ["John","Ryan"]; //Third array - combine first and second array var thirdArray = firstArray.concat(secondArray); console.log(thirdArray); //["PHP", "apache", "javascript", "HTML5", "John", "Ryan"] |
Convert an Array into String in Javascript using toString() Method
Javascript toString() method is used to convert an array into string.
1 2 3 4 5 6 |
//Array declaration var firstArray = ["PHP", "apache", "javascript"]; console.log(firstArray.toString()); // PHP,apache,javascript |
We can also convert an array to string using join() method.
1 2 3 4 |
var firstArray = ["PHP", "apache", "javascript"]; console.log(firstArray.join()); // PHP,apache,javascript |