In JavaScript, if you want to get the index of an element in an array based on its value, you can use the indexOf() method. If the value is not found in the array, the indexOf() method will return -1. So, you can check if the index is -1 to determine if the value exists in the array or not.
Source code viewer
// Sample array var array = [10, 20, 30, 40, 50]; // Get the index of a specific value var value = 30; var index = array.indexOf(value); // Check if the value exists in the array if (index !== -1) { console.log("Index of " + value + " is: " + index); } else { console.log(value + " is not found in the array."); } // This code will output: "Index of 30 is: 2", because 30 is located at index 2 in the array.Programming Language: Javascript