How to Check if an Element is Present in an Array in Javascript

In this video tutorial, you will learn how to check if an element is present in an array in javascript.

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    
    <div>
        <input type="text">
        <button>Check</button>
        <h1>Result</h1>
    </div>

    <script src="script.js"></script>
</body>
</html>

 

CSS:

body {
    text-align: center;
}

div {
    display: inline-block;
}

input,button {
    display: inline-block;
    padding: 10px 20px;
}

 

Javascript:

let users = ['Peter', 'Mary', 'Marks', 'James', 'Ronald'];

let btnCheck = document.querySelector('button');
let input = document.querySelector('input');
let result = document.querySelector('h1');

btnCheck.addEventListener('click', () => {
    // result.innerText = users.includes(input.value) ? 'True' : 'False';

    let myArray = users.map(user => user.toLowerCase());
    result.innerText = myArray.includes(input.value.toLowerCase()) ? 'True' : 'False';
});

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •