In JavaScript, you can use the includes()
method to check if an array includes a particular value or not. Here's an example:
javascriptconst arr = ['apple', 'banana', 'orange', 'grape'];
console.log(arr.includes('orange')); // Output: true
console.log(arr.includes('mango')); // Output: false
In this example, the includes()
method is used to check if the arr
array includes the values 'orange'
and 'mango'
.
The includes()
method takes one argument, which is the value to check for in the array. If the value is found in the array, the method returns true
, otherwise it returns false
.
In the first console.log()
statement, the includes()
method returns true
because the value 'orange'
is present in the arr
array. In the second console.log()
statement, the includes()
method returns false
because the value 'mango'
is not present in the arr
array.
Note that the includes()
method is case-sensitive, so if the value you're looking for is in a different case than the array elements, it will not be found.
JavaScript Interview Questions and Answers, JavaScript, JavaScript Tutorial
Comments
Post a Comment