How To Check If An Array Doesn’t Contain Values In JavaScript

How To Check If An Array Doesn’t Contain Values In Javascript

Are you attempting to test if an array doesn’t include values in JavaScript? Don’t fear; you might be on the proper place. This information will present you many strategies to carry out this activity. Let’s test it out to know extra about these approaches.

Check If An Array Doesn’t Contain Values Utilizing consists of()

To test whether or not or not an array doesn’t include values, make the most of the strategy consists of(). It will test to see if a selected worth is current in an array’s entries and returns true or false relying on the outcome.

Here is the code we use.

consists of(searchElement)

consists of(searchElement, fromIndex)

In this code, searchElement is the worth that you simply need to search. Meanwhile, the fromIndex is the start line for the searchElement operation inside the array. Notice that the consists of() methodology is case-sensitive if you evaluate characters and strings.

[1, 2, 3].consists of(2)         // true

[1, 2, 3].consists of(4)         // false

[1, 2, 3].consists of(3, 3)      // false

[1, 2, 3].consists of(3, -1)     // true

[1, 2, NaN].consists of(NaN)     // true

["1", "2", "3"].consists of(3)   // false

Check If An Array Doesn’t Contain Values Utilizing array.consists of()

You can make the most of array.consists of() to test whether or not an array features a primitive worth. If it does, you’ll obtain the true worth. Otherwise, it returns false.

READ :  React Native with Next.js Backend

Below is an instance of utilizing the strategy array.consists of().

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

var worth = 5;

var isFound = arr.consists of(worth);



    Output: true


In case it’s worthwhile to ignore letter instances when performing the test course of, observe the next step.

  • First, give again a brand-new array with the entire entries in lowercase using the toLocaleLowerCase() and map() strategies.
  • Next, make the most of the strategy consists of() to test.

You can see the above steps employed within the following pattern.

const colours = ['Red', 'GREEN', 'Blue'];

const outcome = => e.toLocaleLowerCase())

                     .consists of('inexperienced');                          

console.log(outcome); // true

As you may see, the array colours doesn’t embrace inexperienced however fairly GREEN worth.

Here, the strategy map() first lowercases every factor of the array colours earlier than returning a unique array. After that, the strategy consists of() offers again the worth true because the array consists of the inexperienced factor.

The Bottom Line

Now you know the way to appropriately test if an array doesn’t include values in JavaScript. Hopefully, you discover our information useful and straightforward to observe. Keep practising, and you’ll be adept at utilizing these strategies very quickly.Suppose you need to study extra JavaScript-related abilities; try our website. We present many tutorials that may provide help to resolve any downside you could have, corresponding to including class to clicked factor or calculating proportion between two numbers.

READ :  TypeError: Cannot read properties of undefined (reading ‘$router’) vuejs

Leave a Reply

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