-1

While stumbling upon JSFuck (an esoteric language that uses only 6 characters []()+! and is perfectly valid javascript code) i saw that in Javascript adding together 2 arrays [] + [] returns an empty string ''. Also in general adding an array converts the expression to a string

for example: false + [] gives 'false'.

Why is this or where does this come from and where can i find a fuller documentation of these peculiar behavior than this documentation of javascript operators . Thanks a lot for the help in advance i'm very curious to expand my understanding of javascript.

j08691
  • 190,436
  • 28
  • 232
  • 252
MrClottom
  • 266
  • 1
  • 12
  • 2
    This is not an arithmetic operation, though - neither of the two values are numeric. – VLAZ May 06 '19 at 21:13
  • I think you may want to take a look at the ECMAScript documentation about the Addition operator and, eventually, the unary operator. I think you should first check the addition operator, though: https://ecma-international.org/ecma-262/5.1/#sec-11.6.1 – briosheje May 06 '19 at 21:18
  • https://stackoverflow.com/questions/4170978/explain-why-this-works – Bergi May 06 '19 at 21:22
  • @Bergi — Could I find that (duplicate)? Noooo, such an unhelpful thing to try to Google. Added to https://stackoverflow.com/a/9550412/19068 for future reference. – Quentin May 06 '19 at 21:25
  • @Quentin I found it via https://stackoverflow.com/search?q=jsfuck and following links from there... – Bergi May 06 '19 at 21:35

1 Answers1

-2

You need to know that before operator '+', javascript in many cases use method 'toString' if operands is not number. Thus, the '+' operator can implement either concatenation or addition.

In the case of at least one non-numeric operand, the operands more often lead to the string and concatenates them.

Here you can find more info type conversion.

console.log(1 + '1' === '11'); // true
console.log(1 + 1 === 2); // true

But keep in mind that toString() does not always work. For some types, cast to the number type and may return an unexpected result.

console.log(1 + NaN); // NaN
console.log(1 + Infinity); // Infinify 
console.log(1 + undefined); // NaN

console.log([1, 2].toString()); // 1,2
console.log([1, 2] + '') // 1,2

console.log(false.toString()); // 'false'
console.log(false + ''); // 'false'

console.log([].toString() === ''); // true
console.log(('' + []) === ''); // true

Array.prototype.toString = () => 'test';
console.log([].toString() === 'test'); // true
console.log(('' + []) === 'test'); // true 
  • 1
    That isn't true - https://jsbin.com/sucijamuzu/1/edit?html,js,console – Quentin May 06 '19 at 21:19
  • 1
    That's not true. It's actually more complex than a simple ToString. – briosheje May 06 '19 at 21:19
  • 1
    Editing in more examples where it *does* cast to a string doesn't change the fact it doesn't *always* cast to a string. – Quentin May 06 '19 at 21:26
  • 1
    "*before operation sum*" but this is *not* a "sum operation". That would be an *arithmetic* operation and string concatenation is *not* an arithmetic operation - it's a completely different thing that shares the same operator `+`. You are giving the incorrect impression that `1 + 1` and `"a" + "a"` perform the same operation when that's not the case - these are two different notions. In the case of mixed types one or the other would be performed depending on the operands but an overloaded operator doesn't make for universally this being the same concept. – VLAZ May 06 '19 at 21:32