...list in javascript code example
Example 1: list javascript
//Creating a list
var fruits = ["Apple", "Banana", "Cherry"];
//Creating an empty list
var books = [];
//Getting element of list
//Lists are ordered using indexes
//The first element in an list has an index of 0,
//the second an index of 1,
//the third an index of 2,
//and so on.
console.log(fruits[0]) //This prints the first element
//in the list fruits, which in this case is "Apple"
//Adding to lists
fruits.push("Orange") //This will add orange to the end of the list "fruits"
fruits[1]="Blueberry" //The second element will be changed to Blueberry
//Removing from lists
fruits.splice(0, 1) //This will remove the first element,
//in this case Apple, from the list
fruits.splice(0, 2) //Will remove first and second element
//Splice goes from index of first argument to index of second argument (excluding second argument)
Example 2: 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 3: how to make a array in javascript
//Im using a let type variable for the example because it's what i reccomend
let exampleArray = ["Example", "Lorem Ispum Dolor", "I think you've got it"]
//Here's how you select a single object in the list
exampleArray[1]
/*In Javascript the arrays start counting at 0 which means i selected the 2nd
object in the list(Lorem Ispum Dolor) so if i wanna select the first array i
must type*/
exampleArray[0]
/*Now let's say you want to display it on the console, like let's say you are
coding in Node.js and you wanna see the value of the array.
P.S: In the example im gonna show all the values of the array.*/
console.log(exampleArray[0, 1, 2]