Check If All Values In Array Are True Using JS

Check If All Values In Array Are True Using JS

During JavaScript web application development, we may encounter a requirement to check if all values in array are true using JS. Let’s read this article to find out the best method for this requirement.

How to check if all values in array are true using JS?

Falsy values in JavaScript include: false, 0, -0, 0n, empty string, null, undefined, NaN and document.all.

In this article, we give three methods with specific cases to help you know how to check if all values in array are true using JavaScript:

  • Use the includes() method.
  • Use the some() method.
  • Use the every() method.

Use the includes() method

The includes() method returns true or false after determining whether an array contains a specified value.

Syntax:

includes(element, start)

Parameters:

  • element: The value to search for.
  • start: The position at which to begin searching.

With the value to search specified as false, the includes() method will return true if the original array contains the false value.

Example:

const arr = [false, true, true];
console.log(arr.includes(false));

Output:

true

Because this article aims to check if all values in array are true using JavaScript, we will modify the code as follows.

Code:

// Check if all values in 'arr' array are true
function allTrue(arr) {
    const isAllTrue =! arr.includes(false);
    return isAllTrue;
}

const arr1 = [true, true, true, true];
const arr2 = [false, true, true, true];
console.log(allTrue(arr1));
console.log(allTrue(arr2));

Output:

true
false

The code will return true if no false values are found in the array and false otherwise.

Use the some() method

The some() method checks if any array element meets the given condition.

Syntax:

some((element, index, array) => { 
    /* … */ } 
)

Parameters:

  • element: The current element being processed in the array.
  • index: The index of the current element being processed in the array.
  • array: The array some() was called upon.

The some() method returns true if the function returns true for one of the array’s elements.

Example:

const arr = [false, true, true];
console.log(arr.some(element => element === false));

Output:

true

We will modify the code in specific case in this article as follows.

Code:

// Check if all values in 'arr' array are true
function allTrue(arr) {
    const isAllTrue =! arr.some(element => element === false);
    return isAllTrue;
}

const arr1 = [true, true, true, true];
const arr2 = [false, true, true, true];
console.log(allTrue(arr1));
console.log(allTrue(arr2));

Output:

true
false

The code will return true if no false values are found in the array. If it is false, it will be the opposite.

Use the every() method

The every() method checks whether all the elements of an array satisfy the argument method.

Syntax:

every((element, index, array) => { 
    /* … */ } 
)

Parameters:

  • element: The current element being processed in the array.
  • index: The index of the current element being processed in the array.
  • array: The array some() was called upon.

The every() method returns true if all the array elements satisfy the argument method.

Example:

const arr1 = [true, true, true];
const arr2 = [false, true, true];
console.log(arr1.every(element => element === true));
console.log(arr2.every(element => element === true));

Output:

true
false

You can also use the Boolean object.

Example:

const arr1 = [true, true, true];
const arr2 = [false, true, true];
console.log(arr1.every(Boolean));
console.log(arr2.every(Boolean));

Output:

true
false

Summary

This article has shown how to check if all values in array are true using JS. I hope the information in this article will be helpful to you. If you have any problems, please comment below. I will answer as possible. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *