Closed Bug 1264308 Opened 8 years ago Closed 8 years ago

Result of expression {}+{} is NaN

Categories

(Core :: JavaScript Engine, defect)

35 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: michalwadas, Unassigned)

References

Details

User Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0
Build ID: 20160407164938

Steps to reproduce:

I have written code:
{}+{}



Actual results:

It returned NaN


Expected results:

It should return string "[object Object][object Object]" as per spec.
Follow steps: https://tc39.github.io/ecma262/#sec-addition-operator-plus
It seems like I have been confused by Chrome Dev Tools - expression `{}+{}` is treated there as `({}+{})`
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → INVALID
(In reply to michalwadas from comment #1)
> It seems like I have been confused by Chrome Dev Tools - expression `{}+{}`
> is treated there as `({}+{})`

Should we do something similar?

We evaluate it as an empty block followed by +{} of course, but it might be confusing, especially when you do:

 < {}
 > undefined
 < {x: 1}
 > 1
Flags: needinfo?(nfitzgerald)
Flags: needinfo?(jorendorff)
Personally i don't want that feature.
I don't have a strong opinion, forwarding to bgrins who has been working on the console.
Flags: needinfo?(nfitzgerald) → needinfo?(bgrinstead)
There's a similar bug about handing input like { 'a': true } in Bug 972530.  It's also resolved invalid and I think it depends on how much convenience we want to provide vs passing input directly to the engine.  In that bug there's a way listed to work around it, similar to:

if (/^\s*\{/.test(text) && /\}\s*$/.test(text))
  text = '(' + text + ')';

I'm more inclined to add support to the JSON input since that seems like a more common situation that will confuse people.  Incidentally, that regex would also match this case though so we'll want to tweak it if we explicitly *don't* want to support this.
Flags: needinfo?(bgrinstead)
See Also: → 972530
This is a longstanding JS wart and I think it's a mistake to try to paper over it. Making a clean spot in one place just makes the rest of the language look shoddy and inconsistent:

    $ node
    > {}+{}
    '[object Object][object Object]'
    > eval("{}+{}")
    NaN
Flags: needinfo?(jorendorff)
User Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.12 Safari/537.36

console
> {} + {}
> "[object Object][object Object]"
You need to log in before you can comment on or make changes to this bug.