Array and Array Methods with Example in JavaScript


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:

let myArray = ['HTML', 'CSS', 'JavaScript', 'ReactJS', 'Bootstrap', 'Tailwind CSS'];

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

let myArray = [1, 2, 3, 4, 5, 6];
console.log(myArray);
// Output: [ 1, 2, 3, 4, 5, 6 ]

let secondArray = new Array(7, 8, 9, 10, 11);
console.log(secondArray);
// Output: [ 7, 8, 9, 10, 11 ]

console.log(myArray[0]);
// Output: 1

console.log(secondArray[4]);
// Output: 11

JavaScript Array Methods with Examples

1. length

It is used to find the length of an array.

Example:

let myArray = ['HTML', 'CSS', 'JS', 'React', 'Angular'];
console.log(myArray.length);

// Output: 5


2. indexOf()

indexOf() javascript method is used to get the Index of the given element.

Example:

let myArray =  ['HTML', 'CSS', 'JS', 'React', 'Angular'];
console.log(myArray.indexOf('CSS'));

// Output: 1


3. isArray(): 

This array method check, whether the given value is an array or not.

Example:

let myArray =  ['HTML', 'CSS', 'JS', 'React', 'Angular'];
console.log(Array.isArray(myArray));

// Output: true


4. push()

This method is used to put the element at the last index of an array.

Example:

let myArray =  ['HTML', 'CSS', 'JS', 'React', 'Angular'];
console.log(myArray.push('Node.JS'));

// Output: 6


5. unshift()

This array method in JavaScript is used to put the element at the first index of an array.

Example:

let myArray =  ['HTML', 'CSS', 'JS', 'React', 'Angular'];
console.log(myArray.push('Frontend Development'));

// Output: 6


6. pop()

pop() array method is used to remove the element with the last index of an array.

Example:

let myArray = ['HTML', 'CSS', 'JS', 'React', 'Angular'];
myArray.pop()
console.log(myArray)

// Output: [ 'HTML', 'CSS', 'JS', 'React' ]


7. shift()

This javascript array method is used to remove the element with the first index of an array.

Example:

let myArray = ['HTML', 'CSS', 'JS', 'React', 'Angular'];
myArray.shift()
console.log(myArray)

// Output: [ 'CSS', 'JS', 'React', 'Angular' ]

8. reverse()

reverse() javascript array method is used to reverse the given array.

Example:

let myArray =  ['HTML', 'CSS', 'JS', 'React', 'Angular'];
console.log(myArray.reverse());

// Output: [ 'Angular', 'React', 'JS', 'CSS', 'HTML' ]


9. concat()

This array method is used to join two arrays in a single array.

Example:

let myArray =  ['HTML', 'CSS', 'JS'];
let secondArray = ['React', 'Angular'];
console.log(myArray.concat(secondArray));

// Output: [ 'HTML', 'CSS', 'JS', 'React', 'Angular' ]


10. includes()

This array method is used to find does the given array contains a specific value or not.

Example:

let myArray = ['HTML', 'CSS', 'JS', 'React', 'Angular'];
console.log(myArray.includes('HTML'));

// Output: true


11. sort()

sort() javascript array method is used to sort the given array.

Example:

let myArray =  [5, 3, 6, 1, 6, 2, 0, 7, 4, 9];
console.log(myArray.sort());

// Output: [ 0, 1, 2, 3, 4, 5, 6, 6, 7, 9 ]



Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad

Ads Section