---
title: "Check an element in an array via jQuery"  
description: "Check an element in an array via jQuery"  
author: "Ethan Karla"  
published: 2021-07-28  
updated: 2023-06-14  
canonical: https://www.mindstick.com/forum/156668/check-an-element-in-an-array-via-jquery  
category: "jquery"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# Check an element in an array via jQuery

How to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) an [element in an array](https://www.mindstick.com/forum/158838/write-a-rust-program-to-find-the-second-smallest-element-in-an-array) via jQuery?

\

## Replies

### Reply by Aryan Kumar

Sure, here is the jQuery code to check if an element exists in an [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net):

Code snippet

```plaintext
// Get the array
var array = [1, 2, 3, 4];

// Get the element to check
var element = 2;

// Check if the element exists in the array
var exists = $.inArray(element, array) >= 0;

// Check the result
if (exists) {
  console.log("The element exists in the array");
} else {
  console.log("The element does not exist in the array");
}
```

This code will get the array and the element to check. It will then use the inArray() method to check if the element exists in the array. The inArray() method will return the index of the element in the array if it exists, or -1 if it does not exist. The code will then check the result and print a message to the console.

Here is an example of how the code can be used:

Code snippet

```plaintext
<!DOCTYPE html>
<html>
<head>
<title>Check element in array</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
// Get the array
var array = [1, 2, 3, 4];

// Get the element to check
var element = 2;

// Check if the element exists in the array
var exists = $.inArray(element, array) >= 0;

// Check the result
if (exists) {
  console.log("The element exists in the array");
} else {
  console.log("The element does not exist in the array");
}
</script>
</body>
</html>
```

When the code is executed, it will print the following message to the console:

Code snippet

```plaintext
The element exists in the array
```


---

Original Source: https://www.mindstick.com/forum/156668/check-an-element-in-an-array-via-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
