Skip to main content

Array in javascript

The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.

JavaScript arrays are written with square brackets.

Array items are separated by commas.

The following code declares (creates) an array called students, containing three items (student names):

const students = ['siva','chan','ruban'];

Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

console.log(students[0]); // this will print 'siva'

Array methods

Array.join();

This example uses the join() method to create a string from the students array.

const students = ['siva','chan','ruban'];
const studentsString = students.join(', ');
console.log(studentsString);
// "siva, chan, ruban"