
Check if value exists in array javascript
Created at 09-Mar-2021 ,
By samar
Check if value exists in array javascript
Through the use of the programming language, we will work together to solve the "Check if value exists in array javascript" puzzle in this lesson.
-
<script> var fruits = ["Apple", "Banana", "Mango", "Lemon", "Orange", "Papaya"]; if(fruits.indexOf("Lemon") !== -1){ document.write("Value exists!"); } else{ document.write("Value does not exists!"); } </script>
The indexOf() method returns the index number of the specified element. If the specified element exists in the array it returns the value (0, 1, 2, 3, and so on) as per the index number of the element. If the element does not exist in the array it returns -1. The indexOf() method is case sensitive. It treats “Lemon” and “lemon” as different values. The IndexOf() method returns -1 on NaN, even (NaN) value exists in the array. But includes() method treats it (NaN) as a different way and returns true. -
<script> var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"]; document.write("[\"Apple\", \"Banana\", \"Mango\", \"Orange\", \"Papaya\"]" + "<br/><br/>"); document.write("True means value exists in the array and false means value does not exist in the array."+ "<br/> <br/>"); document.write("Search Banana : " + fruits.includes("Banana") + "<br>"); // Outputs: true document.write("Search banana : " + fruits.includes("banana") + "<br>"); // Outputs: false document.write("Search Coconut : " + fruits.includes("Coconut") + "<br>"); // Outputs: false document.write("Search Orange : " + fruits.includes("Orange") + "<br>"); // Outputs: true document.write("Search ORange : " + fruits.includes("ORange") + "<br>"); // Outputs: false document.write("Search Cherry : " + fruits.includes("Cherry")); // Outputs: false </script>
Includes method returns true and false as output. It returns true if the specified value exists in the array and returns false if the value does not exist in it. The Includes method is case sensitive. Includes method can be implemented on string also. You can use this method to check if value exists in array or not. javascript includes() method, var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"]; fruits.includes("Banana");.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Javascript
- How to get file extension using javascript/jquery
- ReferenceError: Cannot access 'myFunction' before initialization
- Json stringify
- Uncaught SyntaxError: Identifier 'CODE' has already been declared
- Javascript get only 10 characters from string
- Short-circuits conditionals
- How to display for slash as a string in javascript HTML
- Remove property from object in javascript
- Multidimensional array example javascript
- How to crop multiple images with cropper js
- How to check file is an image in javascript using filename
- Prism js horizontally overflowing its container
- How can I add a key/value pair to a JavaScript object
- Deault order of record in datatable
- Input type file styling
- Cropper js with multiple images
- How to set href value of anchor tag in javascript
- How to add key/value pair to existing form data
- Concat variable with string in javascript
- JavaScript test() Method
- Trim the leading and trailing comma from string in Javascript
- Javascript to print Fibonacci series upto 15 Observations
- How to pass array as arguments to function in javascript
- Uncaught TypeError: $(...).summernote is not a function
- How to get data from localstorage in javascript