26
loading...
This website collects cookies to deliver better user experience
Below the surface of the machine, the program moves. Without effort, it expands and contracts. In great harmony, electrons scatter and regroup. The forms on the monitor are but ripples on the water. The essence stays invisibly below.
2^64
(2 to the power of 64) potential numbers. That equates to approximately 18 quintillion options.-
sign and non-whole numbers like floats. If we consider all of that, we would still have 9 trillion combinations for whole numbers. Unfortunately, not enough to store all the grains of sand...5 + 3
, where the plus symbol takes two values. A unary operator takes one value; hence the name. typeof
is a unary operator that returns the value type.true ? 1 : 2
.null
and undefined
are peculiar types. The author says they are used interchangeably and are more or less the same thing. I can't entirely agree, as I see undefined
as values that could exist later, whilst null symbolises the value's absence. I'd instead stick to just using undefined if I can, but it's always best to secure your types wherever possible. The author also mentioned that:The difference in meaning between undefined and null is an accident of JavaScript's design, and it doesn't matter most of the time.
Quote from the book Professional JS For Web Developers (Wrox): "You may wonder why the typeof operator returns 'object' for a value that is null. This was actually an error in the original JavaScript implementation that was then copied in ECMAScript. Today, it is rationalised that null is considered a placeholder for an object, even though, technically, it is a primitive value."
console.log(8 * null);
// → 0 (null is converted to 0)
console.log('5' - 1);
// → 4 ('5' becomes 5)
console.log('5' + 1);
// → 51 (1 becomes '1')
console.log('five' * 2);
// → NaN (can't use *, /, or - on strings)
console.log(false == 0);
// → true (false becomes 0)
NaN
errors, keep in mind that further arithmetic operations on NaN
keep producing NaN
, so look where you might be doing any accidental type conversions.===
as that allows you to precisely test for equal values and avoids automatic type conversion.