- Octal literal and numeric parsing
console.log(018 - 015);
//5
Key point summary: Octal literal (legacy octal)
- In non-strict mode, numbers starting with 0 may be parsed as octal.
- The digit 8 in 018 is not valid in octal → parsed as decimal 18.
- 015 is a valid octal number → parsed as decimal 13.
- In strict mode it will throw an error; using numbers with a leading 0 is not recommended.
- The return value of the typeof operator
console.log(typeof typeof 1)
//string
Key point summary: typeof always returns a string
typeof 1 returns "number".- Executing
typeof "number" → "string". - The return value of typeof is always a string.
- 0.1 + 0.2 !== 0.3
console.log(0.1 + 0.2 == 0.3)
//false
Key point summary: IEEE754 floating-point precision error
- JavaScript uses the 64-bit floating-point format, which introduces precision errors.
- The actual result of 0.1 + 0.2 is 0.30000000000000004.
- Floating-point equality should be checked using a tolerance:
Math.abs(a - b) < Number.EPSILON.
- sort() sorts in lexicographical order by default
const numbers = [33, 2, 8];
numbers.sort();
console.log(numbers[1])
//33
Key point summary: sort sorts by lexicographical order by default
- When sort() is used without a compare function, it converts elements to strings before comparing.
- This causes numeric sorting to behave unexpectedly, such as “2” < “33” < “8”.
- To sort numerically, use:
sort((a, b) => a - b).
- Implicit type conversion rules of ==
console.log(false == '0');
//true
Key point summary: Abstract equality triggers implicit type conversion
'0' is converted to a number → 0.false is converted to a number → 0.- Therefore, false == ‘0’ → true.
- In real-world development, using
=== is recommended to avoid implicit conversions.
- Sparse Array
let array = [1, 2, 3];
array[6] = 9;
console.log(array[5]);
//undefined
Key point summary: Sparse arrays and empty slots
- Assigning a value to a high index creates empty slots.
- The array structure becomes something like
[1, 2, 3, <3 empty slots>, 9]. - Reading an empty slot returns
undefined, but no actual value is stored internally. - An empty slot is semantically different from
undefined.
- The type of NaN
console.log(typeof NaN);
//number
Key point summary: NaN belongs to the number type
typeof NaN returns "number", as defined by the specification.- NaN represents “a numeric result that cannot be expressed.”
- To check for NaN, use
Number.isNaN(); using === will not work.
- banana trick
console.log(('b' + 'a' + + 'a' + 'a').toLowerCase());
//'banana'
Key point summary: The unary plus triggers numeric conversion
+'a' fails to convert → NaN.- String concatenation: “ba” + NaN → “baNaN”.
- toLowerCase() → “banana”.
- This demonstrates the implicit type conversion behavior of JavaScript’s unary operator.
- Objects undergo ToPrimitive conversion when used with ==
const isTrue = true == [];
const isFalse = true == ![];
console.log(isTrue + isFalse);
//0
Key point summary: Objects undergo ToPrimitive conversion when using ==
[] is converted to a primitive → "", then to a number → 0.true is converted to a number → 1.- Therefore, true == [] → false.
![] evaluates to false, so true == false → false.- false + false → 0.
- Only 6 values are falsy; all others are truthy: false, 0, -0, "" (empty string), null, undefined, NaN.
- instanceof can only recognize “object instances”
console.log("This is a string." instanceof String);
//false
Key point summary: instanceof works only for object instances
- The string literal
"abc" is a primitive value, not an object. - Only
new String("abc") creates an actual String object instance. "abc" instanceof String → false.- Using typeof is more reliable for checking primitive types.