Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
One is a empty code block and an empty array interpreted as a number.
And the other is an empty array plus an empty object, am I right? -
stacked26697yLol, I wonder what an interviewer can deduce from the answers to a question like this.
-
bioDan56227y@peropranav
Although both {} and [] are objects, they are different (one is an array, the othe a key-value map).
When you perform operations like addition to two different data types in JS, JS typecasts the datatypes in order to make sense of the operation you are trying to make. Thats why
> 1 + "2" = "12"
(1 is an number, 2 is a string. So 1 gets converted to a string so the result becomes "12")
In the case of the interviewer question, he assume you knew that and that you're answer will go along the lines of:
in the {} + [] case, the object {} determines to typecast both objects to a number.
So you get 0 + 0 = 0
in the [] + {} case, the [] determines to typecast both objects to a string, so you get something like:
([]).toString() + ({}).toString()
Which translates to:
"" + "[object Object]" = "[object Object]"
Sorry for long post. Hope this helps. -
@bioDan I'm sorry, but your explanation is incorrect. Try ({})+[] and see that it's different.
@danielspaniol is correct. {} in the first expression is an empty code block. +[] uses the unary plus operator, which covers the value to a number, which is why it's a zero. -
bioDan56227y@configurator i see what you're saying, but using the same example you've given, you would expect to get a result containig a number or a string with the number (since you're converting "+[]").
Im not big on JS but i may concede the point, i didnt think about the unary plus option (and i still think {} counts as an empty key value map and not an empty code block). -
@bioDan for why this is an empty block and not an object, see the source for the parser:
https://github.com/v8/v8/...
As you can see, if a statement starts with { it's a block, never an object. This is similar to how functions are always statement type declarations when calls in a statement scope, which is why they have to be wrapped in parenthesis in immediately-invoked function expressions. (function () {})()
For the example ({})+[] there is never a conversion to a number (no unary +), just the addition of two objects which converts them both to strings. -
I went for one a month back and they asked me questions on inheritance and OOPS in C++
-
@abhinavr888 as a web developer you must also know Java for Android, swift and objective-c 😝
-
This sucks. This one time, at interview camp, i was asked the result of:
1=="1"
1===1
To their credit, they asked other stuff, but i left the interview thinking about what info about my coding they could get from that.
Didnt get the job. Got one of those standard emails saying "sorry, but you weren't selected for our job interview.." (yes, my interview didn't qualify me for itself).
Their loss. I am a great coder.
Related Rants
Last week, i was giving my interview for a web development internship!
They asked me
what is the difference between :-
{} +[]
and
[]+{}
:(
Weird parts of JS strikes back :(
undefined
javascript js