Arrays in Javascript

In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.

Declaring an Array

// Arrays must be declared with brackets '[]' and each value must be seperated with commas
var myvar = [2, 4, 6, 8, 10]
// Arrays can store different values and can be mixed
var myvar = ["John Doe", 31, "Oxford University", "Full Stack Developer"]
var myvar = ["John Doe", 31, "Oxford University", "Full Stack Developer"]
console.log(myvar[0])

// The first Value in an Array is '0'
//Arrays always start from '0'

To bring out different Values

var myvar = ["John Doe", 31, "Oxford University", "Full Stack Developer"]
console.log(myvar[0, 1, 2])

/* this should bring out: John Doe, 31, Oxford University */

Arrays can be used to store information that would later be used

var myvar = ["John Doe", 31, "Oxford University", "Full Stack Developer"]
console.log("my name is " + myvar[0] ", I'm a " + myvar[3])