# JavaScript: Array every() method

JavaScript Array every method; Through this tutorial, i am going to show you how to javaScript array.every() for check whether all the given elements in an array are satisfied the given test condition.

## JavaScript Array `every()` method

Javascript Array `every()` method, check whether all the array elements are fulfilled the given test condition. It returns true when each given array element fulfills the test condition, otherwise return false.

In ES5, JavaScript Array `every()` method, tests each element of array with specific test condition with function.

### Syntax of javascript `every()` method

The following represents the syntax of the `every()` method; as shown below:

`arrayObject.every(callback[, thisArg])`

The `every()` method accepts two named parameters: `callback` and `thisArg`. As shown below:

```function callback(currentElement, index, array){
//...
}```

The `callback()` function accepts 3 parameters:

• First, the `currentElement` is the current element of a given array.
• Second, the index is the index  of the current element of a given array
• Third, the `array` is the given array

### Example 1 – JavaScript Determining If All Array Elements Pass a Test

The following example of javascript `every()` method:

```let numbers = [1, 3, 5, 7, 9];
let output = numbers.every(function (e) {
return e > 0;
});
console.log(output);```

Output:

`true`

For example, you have `numeric` array, see the following:

`let numbers = [1, 3, 5, 7, 9];`

The following javascript code satisfies specific test condition on every element of a numeric array:

```let numbers = [1, 3, 5, 7, 9];
let output = true;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] <= 0) {
output = false;
break;
}
}
console.log(output);```

Output:

`true`

### Example 2 – JavaScript Es6 Array `every()` method

By using the JS ES6 arrow functions, the javascript code can be more concise and more readable:

```let numbers = [3, 5, 9, 11, 15];
let result = numbers.every(e => e > 0);
console.log(result); // true```

### Example 3 – JavaScript Array `every()` method

The following example checks whether all the array elements are even numbers in it:

```let numbers = [3, 5, 9, 11, 15];
let isEven = numbers.every(function (e) {
return e % 2 == 0;
});
console.log(isEven);```

Output:

`false`

The following example checks whether all the array elements are odd numbers in it:

```let numbers = [3, 5, 9, 11, 15];
let isOdd = numbers.every(function (e) {
return Math.abs(e % 2) == 1;
});
console.log(isOdd);```

Output:

`true`

If you use the `every()` method on an empty array, the method will always return `true` for any condition.

See the following example:

```let num = [].every(e => e > 0); // any condition
console.log('num:', num);```

Output:

`num: true`

## Conclusion

In this tutorial, you have learned how to use the JavaScript Array `every()` method to checks whether each element of an array and test defined condition.

## More JavaScript Tutorials

Recommended:-JavaScript Arrays
{"ft_lang_label":"__label__en","ft_lang_prob":0.53713477,"math_prob":0.9197656,"size":2821,"snap":"2022-27-2022-33","text_gpt3_token_len":708,"char_repetition_ratio":0.15193468,"word_repetition_ratio":0.1336207,"special_character_ratio":0.27330732,"punctuation_ratio":0.19823009,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9984111,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T21:33:19Z\",\"WARC-Record-ID\":\"<urn:uuid:0e8cdfc1-dbad-45e9-93be-7db2e6f8a82e>\",\"Content-Length\":\"85158\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:587e7110-e3b4-4189-bb12-9d560ef40ab1>\",\"WARC-Concurrent-To\":\"<urn:uuid:9fbf3572-ca59-40ef-a46d-d6bb04ef3dc4>\",\"WARC-IP-Address\":\"172.67.183.157\",\"WARC-Target-URI\":\"https://laratutorials.com/javascript-array-every/\",\"WARC-Payload-Digest\":\"sha1:QQEO4NMM6FP5LWKHAVDQOU5KHUPU3IUB\",\"WARC-Block-Digest\":\"sha1:HFOGZCPK2JR6P4ABFAWQTEPCPFL6KUCD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103645173.39_warc_CC-MAIN-20220629211420-20220630001420-00781.warc.gz\"}"}