
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript: How to check whether an array includes a particular value or not?
In this article, we will be discussing the construction of an array. Once the array is created we will be looking out for a value by checking whether it exists in the array or not.
But before checking for any particular value, let’s see how the arrays are created in JavaScript.
Syntax
let array = [arrayItem1, arrayItem2, arrayItem3 ...]
Once the array is created, let’s see how to search for a specific value from the array. There are multiple approaches to searching for whether an element exists in the array or not. Below we are going to discuss the same.
Approach #1
This is a traditional approach to checking for a value in the array.
In this approach, we will loop over all the values of the array using the for loop and check the equality of each value with the value that needs to be searched.
Once the value is found we will break the loop and return true. Else we will return false if the iteration is completed without searching.
Example 1
In the below example, we are going to loop over the values to check if the desired value exists in the array or not.
# index.html
<!DOCTYPE html> <html lang="en"> <head> <title>ncode & Decode URL</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> let employees_array = [ "steve", "mark", "mike", "bill", "henry", "orion", ]; let valueChecker = (value) => { for (let i = 0; i < employees_array.length; i++) { let current_value = employees_array[i]; if (value === current_value) { return value + " is present at index: " + i; } } return value + " is not included in this array.."; }; console.log(valueChecker("mike")); console.log(valueChecker("casey")); console.log(valueChecker("henry")); </script> <body> </html>
Output
The results can be find in the console −
Approach 2
Other than the traditional approach, we can also use the includes() method provided by JavaScript.
The includes() method checks for a particular value and returns true or false. True is returned when the value exists in the array, else false is returned.
Example 2
In the below example, we will use the includes() method to check if the value exists in the array or not.
# index.html
<!DOCTYPE html> <html lang="en"> <head> <title>Encode & Decode URL</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> let employees_array = [ "steve", "mark", "mike", "bill", "henry", "orion", ]; console.log("Is Mark Exists: " + employees_array.includes("mark")); console.log("Is Orion Exists: " + employees_array.includes("orion")); console.log("Is Michael Exists: " + employees_array.includes("michael")) </script> </body> </html>
Output
Please see the console to find the results −
- Related Articles
- How to check whether a particular key exist in javascript object or array?
- How to check whether a value is a safe integer or not in JavaScript?
- How to check whether a vector contains an NA value or not in R?
- How do I check if an array includes an object in JavaScript?
- How to check whether provided elements in an array have passed a specified condition or not in JavaScript?
- How to check whether an array is a true array in JavaScript?
- How to check whether a key exist in JavaScript object or not?
- How to check whether a number is finite or not in JavaScript?
- How to check whether a NaN is a NaN or not in JavaScript?
- How to check if a Perl array contains a particular value?
- How to check whether an array is a subset of another array using JavaScript?
- How to check whether or not a browser tab is currently active using JavaScript?
- Check whether a number is a Fibonacci number or not JavaScript
- How To Check Whether a Number is an Ugly Number or Not in Java?
- How To Check Whether a Number is an Empire Number or Not in Java?
