7

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:

enter image description here

  • 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?

Paul Benn
  • 1,911
  • 11
  • 26

2 Answers2

4

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.

Odd behaviour of comparison of object literals

Inus Saha
  • 1,918
  • 11
  • 17
  • Why `{}=={}` returning `false` , if your logic is correct ?. – dhaker Jun 22 '18 at 11:43
  • @dhaker the question was originally about the error its getting, but then it has been updated couple of times. i'll check your concern though. – Inus Saha Jun 22 '18 at 12:05
  • Its not about question its about your explanation, which seems to be inconsistent - **Its because the interpreter interprets {} as a block of code not as an object**. – dhaker Jun 22 '18 at 12:40
  • @dhaker i thought like that because if you execute `{};=={}` it gets the same error as `{}==null`. the semicolon terminated the block and it gets the same error. so it leads me to think that an statement is trying to start with `==` and getting the error. – Inus Saha Jun 22 '18 at 12:43
  • @dhaker but yes your concern is a valid one and i may be wrong (i may be missing something) – Inus Saha Jun 22 '18 at 12:45
  • Got a valid answer here - https://stackoverflow.com/questions/50989247/odd-behaviour-of-comparison-of-object-literals/50989638#50989638 – dhaker Jun 22 '18 at 14:23
3

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!

Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
  • To expand on that,I feel like it should be pointed out as to why it makes no sense to compare - **null in javascript literally means non-existent and can't truly be evaluated as it's not an indentifier of a property**. If you want to consider a variable as undefined, then do precisely that and use the `undefined` keyword isntead! More info on this [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) – Samuel Hulla Jun 22 '18 at 11:36
  • 1
    `I tested on safari this is not true.` Did you try this without the `console.log` around it? Because that does make a difference. – TiiJ7 Jun 22 '18 at 11:38
  • `{}=={}` this is returning false, it means we can compare `{}` this with others – dhaker Jun 22 '18 at 11:42
  • if you ask me this {}=={} code no sense. There is a methods for comparing objects but trick is when you just say {} it is a represent namespace but actually is nothing ( Inus Saha - syntax error ). If you say var myObject = {}; then myObject is 'object' empty but regular. – Nikola Lukic Jun 22 '18 at 11:58
  • First you said - **there is no sense for using {} with equality operators.**. Now you are saying - **{}=={} code no sense**. What is sense then ?. I am trying to understand logic behind it but could not get. – dhaker Jun 22 '18 at 12:36