declare variable array javascript code example

Example 1: how to create an array in javascript

let fruits = ['Apple', 'Banana']

console.log(fruits.length)
// 2

Example 2: javascript array read object value in array

var events = [
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 1
  },
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 2
  },
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 3
  }
];
console.log(events[0].place);

Example 3: 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 4: how to make a variable equal something in a array javascript

//How to make a variable equal a specific element in javascript.

/*====DESCRIPTION STARTS HERE=======
Assuming you've already created an array and a variable, you simply set the
variable equal to the array, specifying the specific location of the value
it holds. Note though that when specifying the location, subtract one
number from the actual number we percieve. The reason for this is that computers
begin counting at 0.
  =====DESCRIPTION ENDS HERE========*/

//=====EXAMPLE=========
var listOfFirstNames = ["Will", "Kotori", "Grace", "James"]
var FirstName = listOfFirstNames[2] /*firstName now has the value of "Grace"
									since grace is the 2nd (or 3rd) element
									of the array*/