check if array has true value javascript code example

Example 1: check if array does not contain value javascript

var fruits = ["Banana", "Orange", "Apple", "Mango"];

var n = fruits.includes("Mango"); // true

var n = fruits.includes("Django"); // false

Example 2: javascript array contains

var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red"); //true
var hasYellow = colors.includes("yellow"); //false

Example 3: javascript check if any value in array is true

let boolArray1 = [true, false, false]
let boolArray2 = [false, false, false]

boolArray1.some(x => x);  // true
boolArray2.some(x => x);  // false

// Example of using a function to evaluate array
let numberArray = [1, 2, 3, 4, 5];
let oddNumbers = [1, 3, 5, 7, 9];

// checks whether an element is even
const even = (element) => element % 2 === 0;

numberArray.some(even);  // true
oddNumbers.some(even);   // false

Example 4: how to Check if an array contains a string

// To check if an array contains a string 

const colors = ['red', 'green', 'blue'];
const result = colors.includes('red');

console.log(result); // true

// Or 

const colors = ['Red', 'GREEN', 'Blue'];
const result = colors.map(e => e.toLocaleLowerCase())
                     .includes('green');                          

console.log(result); // true