In this JavaScript tutorial, we are going to learn "Array in JavaScript", "JavaScript Array Example" and Different JavaScript Array Methods with Examples:
What are Arrays in JavaScript?
- JavaScript arrays are similar to a variable that stores different data type variables.
- A JavaScript array can store Primitive and Reference Data Types.
- JavaScript array index always starts from 0. And the last index of the array is "array length - 1".
- Let's see with an example:
JavaScript Array Example:
In the given array example:
- The let keyword is used to declare a variable in JavaScript. In this case, the variable is named myArray.
- myArray is assigned the value of an array using square brackets [].
Javascript array in different ways examples
JavaScript Array Methods with Examples
1. length
It is used to find the length of an array.
Example:
2. indexOf()
indexOf() javascript method is used to get the Index of the given element.
Example:
3. isArray():
This array method check, whether the given value is an array or not.
Example:
4. push()
This method is used to put the element at the last index of an array.
Example:
5. unshift()
This array method in JavaScript is used to put the element at the first index of an array.
Example:
6. pop()
pop() array method is used to remove the element with the last index of an array.
Example:
7. shift()
This javascript array method is used to remove the element with the first index of an array.
Example:
8. reverse()
reverse() javascript array method is used to reverse the given array.
Example:
9. concat()
This array method is used to join two arrays in a single array.
Example:
10. includes()
This array method is used to find does the given array contains a specific value or not.
Example:
11. sort()
sort() javascript array method is used to sort the given array.
Example: