@example ['Hello, world!'].contains('world!') * true * * @return {Bool} Returns bool true/false. */Array.prototype.contains =Array.prototype.contains ||function(value) {for(vari = 0, len = this.length; i < len; i++) {if(this[i] === value) {returntrue; } }returnfalse; }...
functionisFruit(fruitName){letfruits=['Apple','Mango','Pear','Peach'];if(fruits.indexOf(fruitName)>-1){returntrue;}else{returnfalse;}}isFruit('Pear');isFruit('Cat'); Output: truefalse Using the.includes()Function to Check if Array Contains Value in JavaScript ...
JavaScript contains a few built-in methods to check whether an array has a specific value, or object. In this article, we'll take a look at how to check if an array includes/contains a value or element in JavaScript. Check Array of Primitive Values Includes a Value Array.includes() ...
It is the simplest and fastest way to check whether a JS array contains an item or not. The JavaScript Array.indexOf() method checks a JS array for a given value or item, and if it finds the item in the array, it returns its index. If the JavaScript array contains no such values ...
functionmyFunction(total, value) { returntotal + value; } Try it Yourself » JavaScript Array reduceRight() ThereduceRight()method runs a function on each array element to produce (reduce it to) a single value. ThereduceRight()works from right-to-left in the array. See alsoreduce(). ...
If activated 'template', 'content' and 'title' options will be sanitized. whiteList object Default value Object which contains allowed attributes and tags sanitizeFn null | function null Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library...
[i]===element){// Return true if the element is found in the arrayreturntrue;}}// Return false if the element is not found in the arrayreturnfalse;}// Sample arrayarr=[2,5,9,6];// Output the result of checking if the array contains the element '5'console.log(contains(arr,5)...
includes()Check if an array contains the specified element indexOf()Search the array for an element and returns its position isArray()Checks whether an object is an array join()Joins all elements of an array into a string keys()Returns a Array Iteration Object, containing the keys of the ...
Finding a value in an array is useful for effective data analysis. Learn how to discover what a JavaScript Array contains using indexOf, includes, for loop, some methods and more.
Click me to see the solution 27. Retrieve Property Values Write a JavaScript function to retrieve the value of a given property from all elements in an array. Sample array : [NaN, 0, 15, false, -22, '',undefined, 47, null]