JS WTF šŸ¦„ with Math

Written by ferreiratiago | Published 2017/09/26
Tech Story Tags: js-wtf | math | javascript | wtf | mathematics

TLDRvia the TL;DR App

Letā€™s ride on weirdness with JavaScript Math.

max() vsĀ min()

WHAT?max is not bigger than min? NO šŸ˜± but thereā€™s a reason for it.

By definition Math.max([value1[, value2[,Ā ...]]]) returns the largest of the given numbers. However, when no arguments are provided it returns the minimum possible value, which is -Infinity.

As you guessed Math.min() returns Infinity when no arguments are provided, i.e. the highest possible value.

Therefore, when comparing Math.max() > Math.min() is not even fair.

Itā€™s notĀ fair.

-Infinity its way lower than Infinity.

0.1 +Ā 0.2

This is not a problem of JS to be fair. This is floating point math.

Computers can only natively store integers, so they need some way of representing decimal numbers. This representation comes with some degree of inaccuracy. Thatā€™s why, more often than not,Ā .1 +Ā .2Ā !=Ā .3ā€Šā€”ā€Š0.300000000000004.com

The problem lies on the way machines store numeric values (i.e. binary representation.) In the case of integer values, only integers that can be represented as number times the power of two can be exactly represented.

As for rational numbers (such as 0.1, which is 1/10) only values whose the denominator can be represented as a power of two can be exactly represented.

Although 0.1 and 0.2 can be exactly represented in base 10 the same is not true for base 2. When this happens our machines create an approximation of their true value (which results into a repeating value).

Therefore, the result of **0.1 + 0.2** would be a repeating value.

Which in base 10 is represented by 0.30000000000000004.

[] +Ā {}

The + operator has a different behaviour in each example.

It behaves as an addition operator for the expression [] + {}, and as unary operator for the expression {} + [].

When interpreting the expression [] + {} JS identifies an addition operation but before adding both operands it converts them to primitives. This is done by executing the static methodĀ .toString().

As a result we end up with the operation '' + '[object Object]'.

Only at this stage JS knows that the addition operator + has the purpose of concatenating strings. And the final value of '[object Object]' is returned.

Next!

For the expression {} + [], the first operant is a pair of curly brackets (i.e. an empty block.) This empty block means ā€œnothing to doā€. Therefore JS moves forward on executing the expression.

When reaching the + operator it behaves as an unary operator because it has no left operand. Through coercion [] is translated into 0.

true +Ā true

Here, the + operator behaves as an additional operator and adds both operands. However before it can do that it needs to convert them ToNumber.

By definition, ToNumber returns 1 if the argument is true, 0 if the argument is false.

For this reason, our expression true + true translates into 1 + 1, which is 2.

1 < 2 <Ā 3

Spoiler alert: Itā€™s coercion again šŸ˜Ž

Here, it may be useful to go step-by-step for both examples.

1 < 2 < 3

  1. Because JS interprets and executes the expressions from left to right, it will run the expression 1 < 2 first, therefore true < 3;
  2. Next, coercion, ToNumber(true) < 3 which translates into 1 < 3;
  3. Return true (because 1 is lower than 3);

3 > 2 > 1

  1. Because 3 > 2 is true then true > 1;
  2. ToNumber(true) > 1 translates into 1 > 1 (coercion again šŸ‘Œ);
  3. Return false (because 1 is not higher than 1);

ā€˜5ā€™ +Ā 3

By definition the addition operator performs either string concatenation or numeric addition.

When one of the operands is a string, JS converts the other operand to a string and returns the concatenation of both. In the case of '5' + 3, because the left operand is a string the final result is the concatenation of both'5' and ToString(3), which is '53'.

The subtraction operator performs a subtraction with the two operands.

However, when an operator is not of the type Number it is converted ToNumber. For that reason, the string '5' is converted into the number 5 to which is subtracted 3. The final result is 2.

< your weirdness >

If you find any weirdness that should be here, please let me know šŸ‘

Thanks toĀ šŸ»

Be sure to check out my other articles on JS WTF

JS WTF šŸ¦„ with Arrays_Letā€™s ride on some WTF with JavaScript Arrays._hackernoon.com

JS WTF šŸ¦„ with Number_Time to ride on some JavaScript WTF with Number._hackernoon.com


Published by HackerNoon on 2017/09/26