-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')
}

}
Jack
  • 19
  • 4

1 Answers1

1

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

jmkmay
  • 1,441
  • 11
  • 21