You are currently viewing Javascript array

Javascript array

An array in JavaScript is a data structure that stores a collection of items of any data type, such as numbers, strings, or objects. It can be created using square brackets, like this:

var array = [1, 2, 3, 4, 5];

 

You can access individual elements of the array by their index, which starts at 0:

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

You can also use array methods to manipulate the elements, such as push() to add an element to the end, pop() to remove the last element, shift() to remove the first element, and unshift() to add an element to the beginning.

array.push(6);
console.log(array); // Output: [1, 2, 3, 4, 5, 6]
array.pop();
console.log(array); // Output: [1, 2, 3, 4, 5]
array.shift();
console.log(array); // Output: [2, 3, 4, 5]
array.unshift(0);
console.log(array); // Output: [0, 2, 3, 4, 5]

Leave a Reply