Here's some random memos about JavaScript.
-
NaN
is a number (?); operators onNaN
yields intoNaN
except in the + case with a string (as below); -
IMPORTANT: if we write
(x + y)
withx
as a number andy
as a string, it yields into a string (because+
is mainly a string concatenation operator); but if+
is another math operator (eg:*
), it yields into a number; another important case of+
:1 + true
yields into2
becausetrue
(otherwisefalse
ornull
) is converted into1
(0
respectively);1 + undefined
yield intoNaN
;"1" + null
yields into"1null"
; -
beware of precision issues with floating-point numbers (because JS uses binary form to represent decimal numbers internally); try to remove the floating point (multiply by
10eX
whereX
is a positive integer) then calculate, finally then divide by the same multiplier; -
remember the ternary operator:
(condition) ? yes : no
; -
IMPORTANT (of for arrays, strings, in for objects):