Array In JavaScript

LISTING OUT ALL THE VALUES IN AN ARRAY

Array In JavaScript

In this article , we take a quick revision about Arrays in JavaScript.

Introduction Of Array

In other programming language, array is a collection of elements of similar datatype in a single variable name but In JavaScript, Array is an object that is the collection of elements of different datatype in a single variable name i.e., it collect mixture of elements of different data type.

Declaration of array

Three different methods are used to declare an array in JavaScript:-

METHOD 1

Syntax

variable_name array_name= [ element1, element2,....]

// Initializing while declaring
const student_detail= ["Rohit", "Btech", "CS33", 245251, "Delhi"];

METHOD 2

Syntax

variable_name array_name= new Array(element1, element2,....)

// Initializing while declaring
// Array directly using new keyword
const student_detail=new Array("Rohit", "Btech", "CS33", 245251, "Delhi");

METHOD 3

Syntax

variable_name array_name= new Array(length)

// Array constructor using new keyword
const student_detail=new Array(5);
// assign values
student_detail[0] = "Rohit" ;
student_detail[1] = "Btech";
student_detail[2] = "CS33" ;
student_detail[3] = 245251 ;
student_detail[3] ="Delhi" ;

Basic Method To Access The Value Of Array

Syntax: To access all of the element :

console.log(array_name);

console.log(student_detail);

Syntax: To access any particular element of an index:

console.log(array_name[index];

console.log(student_detail[0]);

Replacing Array Element

code:

const student_detail=new Array("Rohit", "Btech", "CS33", 245251, "Delhi");
console.log(student_detail);
student_detail[0] = "Ram" ;
console.log(student_detail);

output:


["Rohit", "Btech", "CS33", 245251, "Delhi"]
["Ram", "Btech", "CS33", 245251, "Delhi"]

Length property of an Array

The property length of an array gives the number of the elements that would be stored in the array.

Syntax:

variable_name name = array_name.length;

Screenshot from 2022-08-27 18-51-28.png

Basic Array Methods In JavaScript

  • concat() Method:

    The concat() Method is used in javascript to combine two or more arrays and give another array.

Screenshot from 2022-08-27 19-09-21.png

  • isArray() Method:

    The isArray() Method is used to check whether the passed argument is an array or not.

Screenshot from 2022-08-27 19-42-19.png

  • at() Method:

    The at() Method take the index value of an array and return the element of that index.

Screenshot from 2022-08-27 19-26-53.png

  • push() Method:

    The push() Method is used to push element at the end of the array.

Screenshot from 2022-08-27 19-48-32.png

  • pop() Method:

    The pop() Method is used to remove element from the end of the array.

Screenshot from 2022-08-27 19-55-06.png

  • unshift() Method:

    The unshift() Method is used to add elements at the front of array.

Screenshot from 2022-08-27 19-52-16.png

  • shift() Method:

    The shift() Method is used to remove elements at the front of array.

Screenshot from 2022-08-27 19-58-47.png

  • splice() Method:

    The splice() Method is used to remove and insert elements from one index to other index i.e., remove or add element at a particular location.

Screenshot from 2022-08-27 20-08-30.png