JAVASCRIPT ARRAY

 


Array are used to store multiple values in a single variable, array define the variable with square brackets. 

An array is a special variable which can hold multiple values. 

Syntax;

 const array_name = [item1, item2, item3,..........];

Example:

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

USING NEW KEYWORD:

Syntax

 const array_name = new Array(item1, item2, item3, .....);

Example:

 const cars = new Array ("Volvo", "BMW", "Hundai", Maruthi");


Accessing Array Elements

You access an array element by referring to the index number:

 const cars = new Array ("Volvo", "BMW", "Hundai", Maruthi");

let car = cars[1];


Output:

BMW

Accessing the firs Array Element

 const cars = new Array ("Volvo", "BMW", "Hundai", Maruthi");

let car = cars[0];


Output:

Volvo

Accessing the last Array Element

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

let car = cars [cars.length -1];


Output: 

Maruthi

Changing an Array Element

This statement changes the value of the first element in cars

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

cars[2] = "Honda";


Output:

volvo BMW Honda Maruthi


ARRAY METHODS:

There are different types of array methods.
  • Array length
  • Array tostring( )
  • Array pop( )
  • Array Push ( )
  • Array shift ( )
  • Array unshift( )
  • Array join( )
  • Array delete( )
  • Array concat( )
  • Array flat9 )
  • Array splice( )
  • Array slice( )

Array length:

The length property returns the length of the Array 

Example: 

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

let size = cars.length;

console.log(size);


Output: 

4

Array toString( ):

This JS Method converts an array to a string of coma separated array values. 

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

var tostring=cars.tostring();

console.log(tostring);


Output: 

volvo, BMW, Hundai, Maruthi

Array join( )

The join() method joins array elements into a string.

It this example we have used " * " as a separator between the elements:

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

console.log(cars.join('*'));


Output: 

4


Array push( )

push method is used to add new elements, pushing items into an array.

The push( ) method adds a new element to an array at the end. 

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

cars.push("Honda");

cars.push("Benz");

console.log(cars);


Output: 

volvo BMW Hundai Maruthi Honda Benz


Array Pop( )

It is used to remove elements and add new elements. Popping items out of an array.

The pop( ) method removes the last element from an array.

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

cars.pop( );

console.log(cars);


Output: 

volvo BMW Hundai


Array unshift( )

The method adds a new element to an array at the beginning, The unshift method returns the new array length:

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

cars.unshift("Honda");

console.log(cars);


Output: 

Honda volvo BMW Hundai Maruthi


Array shift( )

The  method removes the first array element and "shifts" all other elements to a lower index.

 const cars = ["volvo", "BMW", "Hundai", "Maruthi"];

cars.s(hift );

console.log(cars);


Output: 

BMW Hundai Maruthi