[].push() – add an item at a very end of the array :
let add_at_the_end = [1, 2, 3, 4, 5];
add_at_the_end[add_at_the_end.length] = 6;
console.log(add_at_the_end); // [1, 2, 3, 4, 5, 6]
[].unshift() – add an item at the beginning of the array :
let add_at_the_beginning = ["added at the beginning", ...[1, 2, 3, 4, 5]];
// instead of ...*spread*, we could use ["added at the beginning"].concat([1, 2, 3, 4, 5]);
console.log(add_at_the_beginning); // ["added at the beginning", 1, 2, 3, 4, 5]
[].pop() – remove an item at the end of the array :
let remove_at_the_end = [1, 2, 3, 4, 5];
remove_at_the_end.length = remove_at_the_end.length-1;
console.log(remove_at_the_end); // [1, 2, 3, 4]
[].shift() – remove an item at the beginning of the array :
let remove_at_the_end = [1, 2, 3, 4, 5];
remove_at_the_end.reverse.call(remove_at_the_end).length = remove_at_the_end.length-1;
console.log( remove_at_the_end.reverse.call(remove_at_the_end) ); // [2, 3, 4, 5]
Math.min() / Math.max() from array as [native code] :
let array = [300, 15, -20, 0, -101, 404];
let min = array[0]; // set it to zero so every indexed value of m would be read
let max = array[0]; // set it to zero so every indexed value of m would be read
for (let i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
console.log("min of the array: ", min, "max of the array: ", max);
Math.min() / Math.max() from array with .apply() :
let array = [300, 15, -20, 0, -101, 404];
let
min = Math.min.apply(null, array), /* null could be this but Math.min() do not consume this internally */
max = Math.max.apply(null, array); /* null could be this but Math.max() do not consume this internally */
console.log("min of the array: ", min, "max of the array: ", max);
Math.min() / Math.max() from array instead of .apply() use ...spread operator (ES6) :
let array = [300, 15, -20, 0, -101, 404];
let
min = Math.min(...array),
max = Math.max(...array);
console.log("min of the array: ", min, "max of the array: ", max);