JavaScript - Check if array includes value with includes()
A JavaScript code example for checking if an array includes a specified value using includes()
.
The HTML has an output element with id "output" to display the check result.
<p id="output"></p>
The includes()
method can be used to check if an array includes a specified value.
The value to check is passed as the first argument to includes()
.
It returns "true" if the value exists, "false" otherwise.
An optional second start position argument can also be passed to includes()
.
let array = ['Apple', 'Banana', 'Grape'];
// Check if array includes value
if(array.includes('Apple')) {
document.querySelector('#output').innerHTML = 'Value exists';
} else {
document.querySelector('#output').innerHTML = 'Value does not exist';
}
TitleJavaScript - Check if array includes value with includes()
CategoryJavaScript
Created
Update
AuthorYousuke.U