javascript array index code example

Example 1: javasript array indexof

var array = [2, 9, 9];
array.indexOf(2);     // 0
array.indexOf(7);     // -1
array.indexOf(9, 2);  // 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0

Example 2: array from js

//Array.from() lets you create Arrays from array-like objects
//(objects with a length property and indexed elements);
//and also:

//More clearly, Array.from(obj, mapFn, thisArg)
//has the same result as Array.from(obj).map(mapFn, thisArg), 
//except that it does not create an intermediate array.
//Basically, it's a declaration that overrides the length property of the method
//(so that it has to be used with the same name length),
//setting it with the same value of the given variable. 
//The values are still undefined, it's just a different notation. Take a look:

console.log(Array.from(length, (_,i) => i));
// It doesn't works with non-iterables
// In this case we are passing an integer

console.log(Array.from({LENGTH}, (_,i) => i));
// It doesn't work with a property name different from "length"

console.log(Array.from({length}, (_,i) => i));
// It works because overrides the .length property of the array
// The method Array.from() assumes that the property...
// ...is referring to an iterable (also if not declared) 

console.log(Array.from(Array(length), (_,i) => i));
// This is the demonstration of the above assertion
// In this case we are using a declared array through...
// ...an instance of the straight method Array()...
// ...that accepts an integer as value

//in case any one reads this a got this from er0s in edabit

Example 3: get item in array from index

var valueAtIndex1 = myValues[1];

Example 4: array javascript

special_dates = [];
special_dates.push(new Date('2021/02/12').getTime());
special_dates.push(new Date('2021/02/13').getTime());
special_dates.push(new Date('2021/02/14').getTime());

			
for (var i = 0; i < special_dates.length; i++) {
 console.log(special_dates[i]) ;
}

Example 5: access index of array javascript

let first = fruits[0]
// Apple

let last = fruits[fruits.length - 1]
// Banana

Example 6: array of in javascript

The Array. of() method creates a new Array instance from a variable number of 
arguments, regardless of number or type of the arguments. The difference between
Array. of() and the Array constructor is in the handling of integer arguments: Array.