Why JavaScript “++[[]][+[]]+[+[]]” expression returns string “10”

Recently I came over quite interesting expressions ++[[]][+[]]+[+[]] and if you open browser JavaScript console and paste it there it will return string “10”, but how it really works?

This is something that took my mind yesterday and I decided to make it clear for me:

  1. Firstly, +[] in JavaScript is the same as +"" which casts a value to number 0, which is used twice, so if we replace it, we can now see following:
    ++[[]][0]+[0]
    

  2. Now we can split it to two three separate expressions:

    ++ // Increment operator
    [[]][0] // Which returns [], that casts to 0 and then incremented to 1 (++ always returns a number)
    +[0] // Which returns 0
    

  3. [[]][0] means we get the first element from array [[]] which has one element that’s empty array and then increment it by 1, let’s write it in JS:

    [[]][0] // [], or
    [[]].pop() // []
    

  4. Getting 1 from ++[[]][0], as we figured out [[]][0] basically means just empty array or, [], then we have [] + 1 which returns string “1”, and due to ++ always returns number, we have just number 1:

    ++[[]][0] // 1
    ++[[]][0] === +([] + 1) // true
    +([] + 1) === 1 // true
    

  5. And the last part + [0], when we add array to anything in JavaScript, it will concatenate it to string of values with commas:

    0 + [0, 1, 2, 3] // "00,1,2,3"
    [1,2] + [3,4] // "1,23,4"
    
    // Or in our case
    1 + [0] // "10" - Yay!
    

To sum it up, lets describe it in single place:

++ // Increment result of next expression, or make number 1
    [
        [] // Array with single empty array item
    ]
    [ // Use bracket expression to access 0 element of previous array, which is empty array "[]"
        +[] // Cast array to 0
    ]

+ // Unary + Operator, or 1 + [0]

[ // Array with one element of number 0
    +[] // Cast array to 0
]

Find shortest bits sequence or base -2 integer representation

Recently I had experience with solving some teasers from codility.com site, here is one of problems and how I solved it.

So the task is about converting integers into bytes representation, or more simply a number written in base -2. The formula is B[i]*(āˆ’2)i for i = [0..Nāˆ’1] where B is array of bytes and N is length of this array.
For example we have following array [1,0,0,1,1], then to find out actual integer we have to:

1 * (-2)^0 = 1
0 * (-2)^1 = 0
0 * (-2)^2 = 0
1 * (-2)^3 = -8
1 * (-2)^4 = 16

integer = 1 + 0 + 0 - 8 + 16 = 9

So the actual PHP code to get integer from base -2 is:

function int_of_base_neg2($a)
{
	$count = count($a);
	$result = 0;
	for($i = 0; $i < $count; $i ++) {
		$result += $a[$i] * pow(-2, $i);
	}

	return $result;
}

This is quite simple and straightforward. The complex part here is how to do this in opposite way. My solution is not fully written by me, and I’m not sure if it works correct in any situation, but here it is:

function base_neg2_of_int($n)
{
	$result = [];
	while($n !== 0)
	{
		$remainder = (int) ($n % -2);
		$n = (int) ($n / - 2);

		if ($remainder < 0)
		{
			$remainder += 2;
			$n += 1;
		}

		$result[] = $remainder;
	}

	return $result;
}


Finally, my actual task was to convert given array to integer, then turn this integer to negative and convert it back to base -2 bytes array, and here is how to do this:

function solution($A)
{
	$int = int_of_base_neg2($A);
	$neg_int = - $int;
	$result = base_neg2_of_int($neg_int);

	return $result;
}

And this is it. I have to say I’m not very good in this algorithms. That’s maybe what people study in universities for programming, but in real work, especially in Web development I never used any of this kind of algorithms I encountered at tests on codility.com or hakerrank.com.

You can play with this code here: http://ideone.com/Li6B8V