Check if the value exists in Array in Javascript

In a programming language such as JavaScript, there are several techniques available to determine whether a specific value is present within an array. To be more specific, there are numerous approaches to verify if the desired value is found among the elements of an array, which may either be provided by the user or predefined. Let’s explore these methods individually, accompanied by a variety of examples.

indexof method

The indexOf function in JavaScript serves as an effective means to determine if a specific value is present within an array. This function operates based on the concept of index positions. When a value is located within the array, this method returns its corresponding index; if the value is not found, it returns -1. Let’s examine the following code:

Example

<script>    
    var army=["Marcos", "DeltaForce", "Seals", "SWAT", "HeadHunters"];

if(army.indexOf("Marcos") !== -1)
{
        alert("Yes, the value exists!")
} 
else
{
        alert("No, the value is absent.")
}

</script>

Output

Output

Yes, the value exists!

The code provided outputs the specified result because the value in question already exists within the array. It is straightforward to see that the anticipated value is located at index 0. Consequently, the indexof method indicates that the sought value is indeed found in the specified array.

includes method

The includes method is a powerful tool that allows us to determine if a specified value is present within a given array. There are multiple approaches to utilizing the includes method. This method yields a Boolean result, returning true if the value is found and false if it is not present. The includes method can be employed in different manners to check for the existence of a value. Here are some examples to illustrate its usage.

Example

varspecialForces=["BlackCats","Marcos", "Demolishers","HeadHunters"];
var name = specialForces.includes("HeadHunters");

In the method described above, we have established two variables as illustrated. The includes methods yield true since the specific value we are searching for exists within the provided array. Conversely, if that value were absent from the array, the includes methods would likely return false.

An alternative approach to utilizing the includes method involves specifying the index from which the search for the desired element begins, resulting in the output of that index value. Refer to the code provided below for an example.

Example

var actors = ["Hrithik", "SRK", "Salman", "Vidyut"];
var names = actors.includes("Vidyut", 3);

In the code snippet presented above, we initiated the variable "actors" with a specific value. Additionally, we created a variable named "names" that will yield either true or false depending on the outcome of the includes method. The aforementioned code will return true, as the value and the corresponding index number have been accurately set, leading to the expected output.

The preceding examples illustrate several of the built-in methods utilized to determine the existence of an element within an array. Additionally, there is an alternative technique for identifying an element in an array through the use of loops. Let us explore how we can ascertain whether an element is present in an array by employing loops, as demonstrated in the code snippet below.

Using loops

Example

var example_array = ['Rahul','Rajesh','Sonu','Siddhi','Mark','George'];

function checkArray(value,array)
{
  var status = 'Absent';

  for(var i=0; i<array.length; i++)
{
    var name = array[i];
    if(name == value){
      status = 'Present';
      break;
    }
  }
  return status;
}

Output

Output

status: Present
status: Absent

In the code example provided, an array has been established containing various values represented as strings. Additionally, a function has been created in which the variable status is utilized as a string to indicate whether the elements are included within the program. The program's logical sequence involves iterating through each element of the array and verifying its existence. If an element is found within the array, it will output "Present"; otherwise, it will display "Absent" as appropriate.

Summary

JavaScript is a versatile and adaptable programming language, as well as a scripting tool. It possesses a robust, developer-friendly character that allows for multiple approaches to accomplish tasks, ensuring that our rate of learning remains high. In this article, we will explore various methods to determine whether a specific value exists within a given array. We will also examine several techniques and general programming examples that are not only straightforward to grasp but can be executed without extensive prior knowledge. The methods we have utilized in this discussion include indexOf and includes, which are among the most commonly employed functions for locating a specified value within an array. Additionally, we will review loop structures that facilitate value searches through traditional linear search techniques, akin to those used in standard programming paradigms.

Input Required

This code uses input(). Please provide values below: