Javascript: Array for Beginners

const array = [1, 2, 4, 7, 8, 0];console.log(array[0]) // output: 1
console.log(array[2]) // output: 4
console.log(array[5]) // output: 0
console.log(array[6]) // output: undefiled
const underneathAnArray = {
"0" : 1,
"1" : 2,
"2" : 4,
"3" : 7,
"4" : 8,
"5" : 0,
}

Some Commonly Used Methods with Array:

const array = [1, 2, 4, 7, 8, 0];
console.log(array.length) // output: 6
const array = ["These", "are", "some", "strings"]
console.log(array.indexOf("are")) // output: 1
console.log(array.indexOf("i am not here")) // output: -1
const array = [1, 2, 4, 7, 8, 0];
console.log(array.push(9)); // output: 6
console.log(array) // output: [1, 2, 4, 7, 8, 0, 9]
const array = [1, 2, 4, 7, 8, 0, 9]
console.log(array.pop()) // output: 9
console.log(array) // output: [1, 2, 4, 7, 8, 0]
const array = [1, 2, 4, 7, 8, 0];
console.log(array.shift()) // output: 1
console.log(array) // output: [2, 4, 7, 8, 0]
const array = [4, 7, 8, 0];
console.log(array.unshift(1, 2)) // output: 5
console.log(array) // output: [1, 2, 4, 7, 8, 0]
const array = [1, 2, 4, 7, 8, 0];
console.log(array.slice(1, 3)) // output: [2, 4, 7]
const array = [1, 2, 4, 7, 8, 0];
console.log(array.splice(1, 2, 9)) // output: [ 2, 4 ]
console.log(array) // output: [ 1, 9, 7, 8, 0 ]
const array = [1, 2, 4, 7, 8, 0];
const anotherArray = ["These", "are", "some", "strings"]
console.log(array.concat(anotherArray)
// output: [1, 2, 4, 7, 8, 0, "These", "are", "some", "strings"]
const array = [1, 2, 4, 7, 8, 0];
console.log(array.reverse()) // output: [ 0, 8, 7, 4, 2, 1 ]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store