The isArray() method determines if an object is an array.
function myFunction() { var fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; var x = document.getElementById(“demo”); x.innerHTML = Array.isArray(fruits); } |
The forEach() method executes a function once for each element in an array.
var txt = “”; var numbers = [45, 4, 9, 16, 25]; numbers.forEach(myFunction); function myFunction(value) { txt = txt + value + “<br>”; } |
This example multiplies each value in the array by 2:
var numbers1 = [45, 4, 9, 16, 25]; var numbers2 = numbers1.map(myFunction); function myFunction(value) { return value * 2; } |
This example creates a new array containing elements with values greater than 18:
var numbers = [45, 4, 9, 16, 25]; var over18 = numbers.filter(myFunction); function myFunction(value) { return value > 18; } |
This example calculates the sum of all the numbers in an array:
var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduce(myFunction); function myFunction(total, value) { return total + value; } |
This example also computes the sum of all the numbers in an array:
var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduceRight(myFunction); function myFunction(total, value) { return total + value; } |
This example checks whether all values are greater than 18:
var numbers = [45, 4, 9, 16, 25]; var allOver18 = numbers.every(myFunction); function myFunction(value) { return value > 18; } |
This example checks if any values are greater than 18:
var numbers = [45, 4, 9, 16, 25]; var allOver18 = numbers.some(myFunction); function myFunction(value) { return value > 18; } |