16. JavaScript Program to find a number existing given array of numbers or not.
How does this program work?
- In this program we are going to learn about how to find whether the given number is existing in array or not using JavaScript.
- In this program array.includes() function is used to check whether the given value exists in an array or not in JavaScript.
Here is the code
<html>
<head>
<title>JavaScript program to find number existing in given array or not</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter number"/> </td>
</tr>
<tr>
<td> <button onclick="search ()">Submit</button> </td>
</tr>
</table>
<div id="num"></div>
<div id="num1"></div>
</body>
<script type="text/javascript">
function search()
{
var array = [5, 9, 64, 95, 4];
var s = parseInt(document.getElementById ('first').value);
document.getElementById('num').innerHTML=("Given numbers : "+ array);
if(array.includes(s))
{
document.getElementById('num1').innerHTML=("Number is existed");
}
else
{
document.getElementById('num1').innerHTML=("Number is not existed");
}
}
</script>
</html>