0

When I use the or in the if statement, it pretends like there is no ||.

Example:

if (code !== "" || null) {
    document.getElementById("person_name").innerHTML = "Welcome " + name + "!";
} else {
    document.getElementById("person_name").innerHTML = "I'm sorry, but you didn't enter your name.";
}

I am using the window.prompt in JavaScript to alert anyone to put their first name in the box and it should output it. If someone just presses enter without putting anything in the box, it should say, "I am sorry, but you didn't enter your name." If someone exits out of the prompt box, it should say the same thing.

Joshua
  • 426
  • 1
  • 10
  • 18

1 Answers1

1

The != operator has higher precedence than || so your statement reads as "if (code is not equal to empty string) OR null" and null always evaluates to false.

What you rather want is:

if (code !== "" && code !== null) {...
Hubert Grzeskowiak
  • 15,137
  • 5
  • 57
  • 74