Got a query from a collegue. I am not able to answer this question.
var x = '9'; // Line no 1
x=='7' // Line no 2
//doing something with x
Line no 2 is not giving any error.
another scenario
var x == '7';
Browser :
SyntaxError: missing ; before statement
This is because the assignment is always done by single ‘=’.
Question :
- How first scenario executes?
This line is a boolean expression. Its value is calculated and simply discarded. It is encouraged by some people to use yoda-conditions
instead of
So that you don’t accidentally type
if(x = 7)and get yourself a hard to find bug.This
on the other hand, is supposed to be a variable declaration, so the
==operator is illegal here.