JavaScript if statement not behaving as expected

后端 未结 4 1701
情深已故
情深已故 2021-01-26 02:33

Just learning to code JavaScript, trying to learn if statements but my code isn\'t working:

var car = 8;
if (car = 9) {
    document.write(\"your code is not wor         


        
4条回答
  •  忘掉有多难
    2021-01-26 03:04

    = is called assignment operator in JavaScript, it assigns the value of the right hand side expression to the variable on the left hand side.

    You have to use comparison operator instead of assignment operator like this

    if (car === 9)
    

    We have two comparison operators in JavaScript, == and ===. The difference between them is that,

    == checks if the values are the same, but === checks if the type and the value is also the same.

    Go through the wonderful answers, to know more about == and ===

提交回复
热议问题