1

i have created an input field but the logical OR operator is not working as intended. the code is below.

'use strict'


let search = document.getElementById('search');

console.log(search);

let link = document.getElementById('link');
console.log(link);

link.addEventListener('click', doSomething);

function doSomething(ev) {

    console.log(ev.type);
    console.log(ev.target);

    if (search.value == 45 || 35) {

        alert('hi');

    } else {

        alert('you');
    }

}
Shahnawaz Hossan
  • 2,695
  • 2
  • 13
  • 24
DEVZQ
  • 43
  • 4
  • 1
    `search.value === '45' || search.value === '35'` you need to check every value, prferably with same type and strict comparison. – Nina Scholz Nov 23 '20 at 10:07
  • See @NinaScholz 's comment, this is just more explanation if you feel lost. Your function will pop up an 'hi' alert always, because your if-statement is always `true`. Why? `search.value == 45` is true when the value of `search.value` is 45 or '45' (because you use the ˙==˙ operator instead of the strict `===` operator. That is nearly what is I am thinking you wanted to do. But the other condition is just 35, which is a truthy value (not 0) , so you are checking that (`search.value` is 45 or `search.value` is "45" or true) instead of (`search.value` is 45 or `search.value` is 35). – Bence László Nov 23 '20 at 10:43

0 Answers0