53

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 :(

Comments
  • 28
    terrible interview question #1835
  • 9
    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?
  • 6
    @danielspaniol
    {} +[]= 0
    and
    []+{}=[object object]
  • 9
    Lol, I wonder what an interviewer can deduce from the answers to a question like this.
  • 30
    @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.
  • 1
    @bioDan Thanks a lot dear :)!
    highly appreciated :)
  • 3
    @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.
  • 0
    @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).
  • 2
    @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.
  • 1
    @configurator got it, thanks 😎👍
  • 1
    I went for one a month back and they asked me questions on inheritance and OOPS in C++
  • 0
    @abhinavr888 in a web dev interview?
  • 1
    @peropranav yup. Imagine expecting that
  • 1
    @abhinavr888 😂😂😂😂!
  • 1
    @abhinavr888 as a web developer you must also know Java for Android, swift and objective-c 😝
  • 3
    @gitpush sure. Might as well at Firebase/Kotlin while we're at it 😄
  • 0
    @abhinavr888 it never hurts knowing more languages 😃
  • 0
    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.
Add Comment