Check if the value exists in Array in Javascript10 Apr 2025 | 4 min read In a programming language like Javascript, to check if the value exists in an array, there are certain methods. To be precise, there are plenty of ways to check if the value we are looking for resides amongst the elements in an array given by the user or is predefined. Let's discuss these methods one by one using various examples. indexof() methodThe indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise. Let's consider the below code: Output Yes, the value exists! The above code prints the given out because the value is already present in the array. It is quite easy to understand that the expected value is present at position 0. Thus, the indexof() method tells you that the value expected is present in the given array. includes() methodThe includes() method is one such method using which we can easily find out whether the expected value exists in the given array. There are various ways to use include() method. This method returns a Boolean value i.e. true if the value exists and false if it incorrect. The includes() method can be used in various ways to find out if the value exists. To name a few, take a look at the below examples to understand. In the above method, we have defined two variables as shown. The includes() methods return true because the value which we are looking for is already present in the given array. If the value was not present in the array, the includes() methods might have returned false. Another way of using the includes() method is by assigning the index value through which the element we are looking for is generated as output. See the below code for reference. In the above code snippet, we have defined the variable "actors" which the value. We have also defined a variable "names" which would return true or false, if the includes() method returns the shown result. The code above will return true since the value and the index number have been correctly assigned and would return the output. The above examples are some of the predefined methods that we have used to check whether an element exists in the array or not. We have another approach to find out an array element using loops. Let's discuss how can we check if the element exists in an array using loops as shown in the below code snippet. Using loopsOutput status: Present status: Absent In the above code snippet, we have defined an array with some values in the form of strings. We have also defined a function under which the variable status is used as a string to mark if the elements are present in the program. The logical flow of the program is to traverse through each element present in the array and check if the element is present. If the value exists in the array it will display "Present" and "Absent" accordingly. SummaryJavascript is dynamic and flexible programming as well as a scripting language. It is a powerful developer-friendly natured language that lets you do single stuff in multiple ways so that our learning curve remains steep. In this article, we discussed how we can easily carve different ways through which we can easily find whether the given array consists of the desired value or not. We also came across certain methods and generic programming examples that are not just easy to understand but can be implemented with no absolute knowledge. We have used indexof() and includes() methods in this article since they are the most used methods whenever it is required to find out the value enshrined in an array. We also came across loops through which one can easily find out by the normal linear search traversal as we do in the generic programming paradigms. Next TopicBootstrap-javascript |
In JavaScript, let and var are two keywords which are used to declare new variables. We will learn the difference between let and var properly in this article. At the beginning of the JavaScript, the var keyword was utilized to declare a variable in the program. After...
5 min read
In JavaScript, we can check whether a string is empty or not using several methods. Here are a few examples: 1. Using the length property: We can use the length property of a string to check whether it has any characters or not. If the length is...
3 min read
The javascript regex modifier controls and operates input data with the metacharacters and other regular expressions. The modifier works with the metacharacter and quantifiers to get the particular field. The search(), match(), test(), exec() method works with the regex modifier in javascript. The three modifiers...
7 min read
? 1. The Fundamentals of IIFE 1.1 Overview In JavaScript, a function referred to as an Immediately Invoked Function Expression (IIFE) is known immediately upon definition. An on-the-spot invocation or execution of a feature expression defines an IIFE, as counseled by its call. Regular features, alternatively, need to be...
9 min read
Introduction Data structures and algorithms structure the foundation of software engineering and programming. They are fundamental apparatuses for taking care of complicated issues productively and are urgent in building vigorous and versatile applications. In this masterclass, we will dive into the universe of JavaScript algorithms and data...
19 min read
What is Selenium in JavaScript? In JavaScript, selenium is a browser automation library. It is most often used for testing web applications, Selenium may be used for any task that requires automating interaction with the browser. It is an open-source web testing framework that is used for automated...
6 min read
Vectors are basic mathematical objects defined by their size and direction. In mathematics, a vector is a depiction of a magnitude with both size and direction and is usually displayed as an arrow on a graph. Vectors are commonly utilized in computer science and programming, particularly...
7 min read
? This article explains interpolation and shows you how to apply it to the strings to increase their tensile strength. Additionally, you will comprehend how to operate string interpolation to improve programming code. Lastly, you will see several code examples to demonstrate the use of JavaScript strings...
4 min read
There is no need to wait for style sheets, graphics, or sub frames to finish loading before the DOMContentLoaded event occurs after the main HTML content has been fully loaded and processed. The event is non-cancelable and generic in JavaScript. When referencing elements in the body...
3 min read
Arrays are one of the most helpful facilities in JavaScript that are utilized for storing various elements. Sometimes, a typical task is replacing the existing elements with new ones. Either based on specific conditions or simply using the opportunity to modify the elements at selected indices,...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India