js array[-1] code example

Example 1: javascript array

//create an array like so:
var colors = ["red","blue","green"];

//you can loop through an array like this:
for (var i = 0; i < colors.length; i++) {
    console.log(colors[i]);
}

Example 2: js array

var colors = [ "red", "orange", "yellow", "green", "blue" ]; //Array

console.log(colors); //Should give the whole array
console.log(colors[0]); //should say "red"
console.log(colors[1]); //should say "orange"
console.log(colors[4]); //should say "blue"

colors[4] = "dark blue" //changes "blue" value to "dark blue"
console.log(colors[4]); //should say "dark blue"
//I hope this helped :)

Example 3: javascript array

const arr = [1, 2, 3];

Example 4: array -1 javascript

//The index of any item that does not exist in an array is -1

//example:
a = ["help","I'm","a","sentient","array"];
a.indexOf("Icecream");//this returns -1

Tags:

Java Example