If I open my browser console (tested in Chrome/Firefox) and type:
null == {}
I get:
false
However, if I commute both arguments to the ==
operator and instead type:
{} == null
I get:
Uncaught SyntaxError: Unexpected token ==
Image:
- Why does this happen?
- Why does this only happen in a console and not when the browser executes a script within an HTML page?
EDIT:
While question 35812626 addresses this and explains the cause as JS parsing the {}
as a code block, it uses the triple equals (strict comparison) operator ===
, not the double equals ==
. As a user points out below, a code block can definitely be followed by ==
without causing a syntax error:
{} == {} // false
How come this works and my example does not?
I think its because the interpreter interprets {}
as a block of code not as an object.
so your code {} == null
turns out to be a block start and end then a statement starts with ==
which is definitely a syntax error.
but if you can try ({} == null)
I think it should run fine.
and as pointed out by @dhaker {}=={}
returning false
not an error.
and i found there are few scenario returning result and few getting error.
following are getting error:
{}==null //Uncaught SyntaxError: Unexpected token ==
{}==1 //Uncaught SyntaxError: Unexpected token ==
{}==0 //Uncaught SyntaxError: Unexpected token ==
{}==true //Uncaught SyntaxError: Unexpected token ==
{}==false //Uncaught SyntaxError: Unexpected token ==
{}==true //Uncaught SyntaxError: Unexpected token ==
{}=='' //Uncaught SyntaxError: Unexpected token ==
{}=='hi' //Uncaught SyntaxError: Unexpected token ==
{}==(new Object) //Uncaught SyntaxError: Unexpected token ==
following are returning result without an error:
{}=={} //false
{}==function(){} //false
so i guess it has something to do with how the Javascript is compiled or interpreted by the browsers.
for much detailed answer please check following answer.
I tested on safari this is not true.
null is primitive variable and null can be on left side.
But {} for javaScript is namespace. Used for creating global object.
Answer is simple: there is no sense for using {} with equality operators.
Updated :
It is a so clear :
Anything in javascript what comes with { have no assignment .
Let try to say :
{ x : null }
and what you will get ?
Nothing . Can you access this object again ? No.
console.log( null == {} );
console.log( {} == null );
console.log( null === {} );
console.log( {} === null );
// Also
var object1 = {'key': 'value'}
var object2 = {'key': 'value'};
console.log(object1 === object2); //false
console.log({} === {}); //false
console.log({} == {}); //false
({}) == null; // little hack ;) false
// {} == null; syntax error !
(typeof {}) == (typeof {});
var A = {};
var B = {};
(typeof A) === (typeof B);
// console.log( (A instanceof "object" ) );
console.log( typeof A );
// This is only correct
console.log(typeof A === typeof B)
console.log(typeof {} === typeof {})
I try this stupid thing :
{'test':0}.hasOwnProperty('test')
and i get same staff ( syntaxError ) . Don't use it like that!
来源:https://stackoverflow.com/questions/50986812/why-does-commuting-the-arguments-of-in-a-console-change-the-output