Common Array Operations in JavaScript
Length of an array
Find length of an array using the length
property.
const array = [3, 5, 6, 7, 9];
array.length; // 5
Maximum value in array
Use Math.max
to find maximum value in an array.
const array = [34, 41, 56, 6, 78, 12];
Math.max(...array);
// 78
// or if you cannot use the spread operator, use apply method
Math.max.apply(null, array);
// 78
Minimum value in array
Use Math.min
to find minimum value in an array.
const array = [34, 41, 56, 6, 78, 12];
Math.min(...array);
// 6
// or if you cannot use the `...` spread operator, use apply method
Math.min.apply(null, array);
// 6
Clear or empty an array
You can empty an array by setting it's length
property to 0
.
const array = [1, 2, 3, 4, 5];
array.length = 0;
console.log(array);
// []
Last updated on Thu Jan 07 2021
Tags: