14

I got this question from Interview,

[1,2] + [4,5,6][1]

JavaScript giving answer 1,25.

How it's happening? Please explain clearly.

KarSho
  • 5,478
  • 13
  • 41
  • 75
  • 14
    [1,2] + [4,5,6][1] = [1,2] + 5 = "1,2" + 5 = "1,25" – BoltClock Apr 25 '15 at 16:45
  • it's a damn good question. Too bad it wouldn't get good exposure due to the uniqueness of the title – vsync Apr 25 '15 at 16:56
  • @vsync any suggestion for title? – KarSho Apr 25 '15 at 16:58
  • 1
    @vsync: There are dozens if not hundreds of these obscure type coercive questions. Simple answer is always the same... JS operators work on all values. Values that don't make sense are usually coerced to some other value. Referencing the ECMAScript spec is a crucial step that many people skip. –  Apr 25 '15 at 17:14
  • 1
    So, you are actual question seems to be why `[4,5,6][1]` returns `5`. That's because you are accessing the second element of the array. That's not really surprising I hope. – Felix Kling Apr 25 '15 at 17:16
  • who says it "opinion-based" ??!!! – Grijesh Chauhan Apr 25 '15 at 17:44
  • @FelixKling - actually that is the easiest part of this "riddle". It is the illogical part of type-casting the left side of the operator which is non-trivial – vsync Apr 25 '15 at 18:39
  • @vsync: I know. But the OP said, before the edit, *"I know `[1,2]+5 => 1,25`. Here **[4,5,6][1] => 5 ?**"*. So I assumed they know what happens to the first operand. – Felix Kling Apr 25 '15 at 19:49

3 Answers3

15

Lets start with the last part and write it more verbose

var arr   = [4,5,6];
var value = arr[1];

Is the same as

[4,5,6][1]

and as arrays are zero based, that gives 5, so we really have

[1,2] + 5;

The first part is equal to

[1,2].toString(),

and that returns the string "1,2" because the array is converted to a string when trying to use it in an expression like that, as arrays can't be added to numbers, and then we have

"1,2" + 5

Concatenating strings with numbers gives strings, and adding the strings together we get

"1,25"
adeneo
  • 293,187
  • 26
  • 361
  • 361
  • why would `[1,2]` be represented as a String? – vsync Apr 25 '15 at 16:48
  • 3
    Because JS coerces the array to something it understands the `+` operator for. – Dan Prince Apr 25 '15 at 16:48
  • 1
    I had no idea the `+` operator affects things left to it – vsync Apr 25 '15 at 16:49
  • @vsync Because you can't represent `[1,2]` as a number – Ismael Miguel Apr 25 '15 at 16:49
  • but why would a language do that? it would be logical to give an error instead of converting the left side to a string. that is just craziness right there – vsync Apr 25 '15 at 16:50
  • 1
    JavaScript generally tries to convert stuff to other stuff it understands, mostly strings, which is why one can do `"3" + 3` and get `"33"` – adeneo Apr 25 '15 at 16:51
  • 6
    @vsync: Given your experience with JavaScript, I'm surprised you (still) think any of this is crazy. – BoltClock Apr 25 '15 at 16:51
  • @BoltClock - coding in JS for over a decade and still am surprised to discover new things now and then. My specialty in JS does not include instantiates, which I try to avoid. Anyway, you gave a good answer. – vsync Apr 25 '15 at 16:55
  • 3
    @vsync: Dynamic typing is one of the core aspects of JS - which is why I was surprised this was new to you. Still, knowing JS, it *always* finds ways to surprise us. – BoltClock Apr 25 '15 at 16:58
7

It breaks down like this:

Starting with:

[1,2] + [4,5,6][1]

First, each side gets evaluated, and since the right-hand side is an array initializer and lookup, it comes out to 5:

[1,2] + 5

Now the + operator starts its work. It isn't defined for arrays, the first thing it does is try to convert its operands into either strings or numbers. In the case of an array, it'll be a string as though from Array#toString, which does Array#join, giving us:

"1,2" + 5

When you use + where either side is a string, the result is string concatenation.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
2

First, [4,5,6][1] evaluates to the number 5

Then, the + operator is being applied to a first argument which is an Array and not a Number, so javascript assumes you're doing a string concatenation, not addition. Your array [1,2] becomes a string, which is "1,2". You can see this yourself with [1,2].toString().

The number 5 is now being appended to a string, so it to gets converted to a string, and appended together to get "1,25".

Andrew Lavers
  • 7,653
  • 1
  • 29
  • 48