0

I'm just learning JS and am confused as to why this if/else statement isn't going to the "else."

If you put in Monty or Chip, you get prompted for the password, if you put in Frank (or anything else) you STILL get prompted for the password, though it does say wrong login or password.

What am I missing to make it go to the "else" if you put in "Frank" or whatever as the login?

var password
var login = prompt('Enter your login.');


if (login === 'Monty' || 'Chip') {
    password = prompt('Enter your password.');
  if (login === 'Monty' && password === 'Cheese') {
    alert('Access Granted Monty');
  } else if (login === 'Chip' && password === 'Gadget') {
    alert('Access Granted Chip');
  } else {
    alert('Wrong login or password!');
  }
} else {
  alert('Wrong login!')
}
4
  • 5
    login === 'Monty' || 'Chip' always return true Commented Aug 26, 2015 at 17:29
  • 'Chip' will return true. That evaluation is only verifying the left side. Commented Aug 26, 2015 at 17:30
  • you can use this...if (login == 'Monty' || login == 'Chip') Commented Aug 26, 2015 at 17:32
  • Thank you all, its obvious now, especially seeing as how in the if/else if statements I got it right. Thank you! Commented Aug 26, 2015 at 18:38

3 Answers 3

7

There is a small error in your code. You need to update your if statement

from

if (login === 'Monty' || 'Chip') {

to

if (login === 'Monty' || login === 'Chip') {
Sign up to request clarification or add additional context in comments.

Comments

3

JavaScript is weird beast in some respects:

This:

if(login === 'Monty' || 'Chip')  

is really doing

login === 'Monty' || 'Chip' is not null or undefined

In JavaScript there is a concept of truthy, which is if a value can be converted into a truthful statement, then it is true. if('Chip') {alert('HI');} would always execute, because the constant 'Chip' will never be null or undefined. In other languages that are stricter about conditionals this wouldn't be allowed, but in JavaScript this is considered correct syntax and is therefore evaluated.

What you want to do is: login === 'Monty' || login === 'Chip'

Comments

3

In the line:

if (login === 'Monty' || 'Chip') {

What actually happens is:

  • comparison of variable login and string 'Monty'
    • if this is true parser enters an if statement
    • otherwise parser converts string 'Chip' to boolean. Because it isn't an empty string, this will always result in true.

You can read more about boolean conversions here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

To fix this try:

if (login === 'Monty' || login === 'Chip') {

This code will compare both strings with login variable before entering an if statment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.