How to Check a Value Exist at Certain Array Index in JavaScript ?
Determining if a value exists at a specific index in an array is a common task in JavaScript. This article explores various methods to achieve this, providing clear examples and explanations to enhance your coding efficiency.
Below are the different ways to check if a value exists at a specific index in an array:
Table of Content
- Using the typeof operator
- Using the in operator
- Using the hasOwnProperty() method
- Using lodash _.includes() method
- Using the indexOf method
1. Using the typeof operator
In JavaScript, the typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable. If the value is undefined, then it doesn’t exist.
Syntax:
typeof (operand);
Example: In this example, we will see the use of typeof operator for checking the value.
let arr = [1, 2, 3, 4, 5];
let value = 3;
if (typeof arr[value] !== "undefined") {
console.log(`Value ${value} exists
at index ${value - 1}`);
} else {
console.log(`Value ${value} doesn't
exist at index ${value - 1}`);
}
Output
Value 3 exists at index 2
2. Using the in operator
The in operator checks if a given property or index exists in the object. For an array, you can use the in operator to check if an index exists or not.
Syntax:
prop in object
Example: In this example, we will see the use of in operator.
let arr = [1, 2, 3, 4, 5];
let n = 3;
if (n in arr) {
console.log(`Value ${n} exists
at index ${arr.indexOf(n)}`);
} else {
console.log(`Value ${n} does not
exist in the array`);
}
Output
Value 3 exists at index 2
3. Using the hasOwnProperty() method
The hasOwnProperty() method is used to check if an object has a specific property or index. In the case of an array, you can use this method to check if an index exists.
Syntax:
object.hasOwnProperty( prop )
Example: In this example, we will see the use hasOwnProperty() method.
const arr = ['w3wiki', 'GFG', 'gfg'];
if (arr.hasOwnProperty(1)) {
console.log(arr[1]);
console.log('The index exists in the array')
} else {
console.log('The specified index does NOT exist');
}
Output
GFG The index exists in the array
4. Using lodash _.includes() method
Lodash _.includes() method is used to find whether the value is in the collection or not. If the collection is a string, it will be tested for a value sub-string, otherwise SameValueZero() method is used for equality comparisons. If the index is given and is negative, the value is tested from the end indexes of the collection as the offset.
Syntax:
_.includes( collection, value, index );
Example 1: In this example, we are checking whether the given value is present in the given collection or not by the use of the _.includes() method.
// Requiring the lodash library
const _ = require("lodash");
// Collection of string
let name = ['gfg', 'Beginner',
'computer', 'science', 'portal'];
// Check value is found
// or not by _.includes() method
console.log(_.includes(name, 'computer'));
// Check value is found or
// not by _.includes() method
console.log(_.includes(name, 'geeeks'));
// Check value is found or
// not by _.includes() method
console.log(_.includes(name, 'gfg', 2));
Output:
true
false
false
6. Using the indexOf method
In this approach we are using the indexOf method. this method searches the array for the specified value and returns the index of the first occurrence of the value if it exists in the array otherwise it returns -1 if the value is not found.
Example: In this example, we are checking whether the given value is present in the given array or not by using indexOf method.
let array = [1, 2, 3, 4, 5];
let value = 4;
let index = array.indexOf(value);
if (index !== -1) {
console.log(`Value ${value} exists at index ${index}.`);
} else {
console.log(`Value ${value} does not exist in the array.`);
}
Output
Value 4 exists at index 3.
Contact Us