The subtraction operator

The subtraction operator (-) does what it says on the tin. It takes two operands, subtracting the right-side operand from the left-side operand:

555 - 100; // => 455

If either operand is not a number, it will be coerced into one:

'5' - '3'; // => 2
'5' - 3; // => 2
5 - '3'; // => 2

This includes non-primitive types too:

[5] - [3]; // => 2

Here, we're seeing two arrays, each with a single element, being subtracted from each other. This seemingly makes no sense until we recall that the primitive representation of an array is its joined elements as a string, that is, "5" and "3" respectively:

String([5]); // => "5"
String([3]); // => "3"

These will then be converted into their numerical representations, 5 and 3, via an operation that is equivalent to the following:

Number("5"); // => 5
Number("3"); // => 3

Therefore, we are left with the intuitive operation of 5 minus 3, which we know is 2

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.46.130