Example javascript array and all method with example.

1 Replies, 482 Views

Example javascript array and all method with example.
A: An example of a JavaScript array is:

var myArray = [1, 2, 3, 4, 5];

The all() method checks if all elements in an array pass a test (provided as a function). The method returns true if all elements in the array pass the test, otherwise it returns false.

Example:

function checkPositive(element) {
return element > 0;
}

var array1 = [1, 2, 3, 4, 5];

console.log(array1.every(checkPositive));
// expected output: true
(This post was last modified: 11-06-2023, 01:07 PM by mohan.)



Users browsing this thread: 1 Guest(s)