Time to ride on some JavaScript WTF with . Number ā0.0ā When converting a into a , JS is able to . Thatās why the get as the result for the first two expressions. String Number interpret a **period** before or after a numeric value as the period of a decimal number 0 Both and represent the decimal . '0.' '.0' 0.0 Hereās the : specification I know. I know. Too muchĀ detail! However, āi_f the grammar_ ā, which explains the result for . cannot interpret the String as an expansion of StringNumericLiteral , then the result of ToNumber is NaN . Number('.') i.e. the expression does not match any of the highlighted in the red box above. '.' string literals {} vsĀ [] When is applied to an JavaScript tries to convert it to a . Number Object primitive value By , it first executes . specification .valueOf() Because of both and also return an object, JS tries . .valueOf() {} [] .toString() . The reason for this WTF lies on the result of .toString() The operation actually runs which is . However, as we saw before, converting a String into a Number returns when it cannot be interpret into a numeric value_._ This explains out first result . ({}).toString() Object.prototype.toString() by definition "[object Object]" NaN Number({}) // NaN As for , it executes and outputs . , is . [].toString() Array.prototype.toString() "" By definition Number("") 0 Therefore is . Number([]) 0 š Ok! Letās take a break and watch this kitties undefined vsĀ null According both and represent the absence of something, meaning . ToBoolean null undefined false However, we can see as more generic āabsence.ā This is because it is used to represent a variableās value when no other value has been assigned. undefined , undefined a variable has been declared but no formal value has been assigned. , on the other hand, is an . It can be assigned to a variable as a representation of āno value.ā null assignment value null, a variable has been declared but has an emptyĀ value. Moreover, they have a different . typeof Because of this I came to that this is why is converted into a when executing , meaning . believe undefined NaN Number(undefined) no value assigned And to the value , meaning . null 0 a representation of a value falsy MIN_VALUE Itās easy to understand that is bigger than but itās weird to realise that is not smaller than . [Number.MAX_VALUE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE) 0 [Number.MIN_VALUE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_VALUE) 0 Thatās because is not actually the minimum value possible**,** but the , which is a ( to be specific), however bigger than . Number.MIN_VALUE minimum positive value possible very very very small value 5e-324 0 To represent the minimum value possible we can safely use i.e. [Number.MIN_SAFE_INTEGER](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER) -(2^53ā1) toFixed() Having a on a numeric value is the way to say that it has a However, this makes the use of inconsistent. period fractional part. static methods For the expression , because the numeric value is a valid number, JS moves forward on interpreting the full expression. However, it finds , which has (as opposed to the expression , which represents the execution of a static method.) and throws a syntax error. 42.toFixed(2) 42. toFixed(2) no meaningful value .toFixed(2) Even a space between and does not solves the problem. 42. toFixed(2) However, a space between and solves the problem. Or even a double period š® 42 .toFixed(2) 42..toFixed(2) 42.toFixed(2)Ā !== 42..toFixed(2) Nevertheless, this is how JS interprets the code. < your WTFĀ > š If you find any WTF that should be here, please let me know Thatās all forĀ Number. Thanks toĀ š» MDN Documentation for his and Kyle Simpson video book All the kitties in the world for publishing ā¤ļø Hacker Noon Be sure to check out my other articles on JS WTF _Letās ride on some WTF with JavaScript Arrays._hackernoon.com JS WTF š¦ with Arrays
Share Your Thoughts