-1

While I have a bit of experience in coding, I am very confused about why this code runs the wrong line of code. If you run this code in any compiler it returns 'hello' even though x is not equal to 4. If someone could explain, that would be great! Thanks.

let x = 5;
let y = 6;

while (x=5) {
   if (x=4) {
       console.log('hello');
       break;
}
   else if (y=6) {
        console.log('how are you?')
        break
} else {
        console.log('potato')
}

}
2
  • 1
    Well, for starters the indentation :) Make life easier on yourself. Commented Nov 12, 2020 at 19:53
  • 2
    using = in place of == Commented Nov 12, 2020 at 19:55

1 Answer 1

1

Well x=4 will evaluate to true, because its an assignment operation. You're looking for x==4.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.