JavaScript Quirks_ Comprehensive Guide
JavaScript Quirks_ Comprehensive Guide
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
1
Exercise: 3
3. Global this 3
Example: Global this in Browsers 4
Example: Global this in Node.js 4
4. Floating-Point Math 4
Example: Precision Errors 4
Fix: 4
5. Adding Arrays and Objects 4
Example: Adding Arrays 4
6. Function Scoping with var 4
Example: Scope of var 5
7. Immutability of const 5
Example: Mutable const Objects 5
8. Automatic Semicolon Insertion 5
Example: Semicolon Misplacement 5
Exercises 5
Exercise 1: Predict typeof Output 6
Exercise 2: Floating-Point Fix 6
Exercise 3: Equality Check 6
Multiple-Choice Questions 6
Question 1: 6
Question 2: 7
Question 3: 7
Best Practices 7
JavaScript is known for its quirks—unusual, sometimes unintuitive behaviors resulting from its
design and flexibility. This guide explains some of JavaScript's quirks with examples,
explanations, exercises, and quiz questions to help you understand and navigate them
effectively.
1. typeof Quirks
The typeof operator is used to determine the type of a variable, but it has some
inconsistencies.
Explanation:
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
2
● null is a primitive value but is incorrectly classified as "object" due to a bug in the
original JavaScript implementation.
Explanation:
● NaN (Not-a-Number) is technically of type number, and isNaN tries to coerce its
argument to a number before checking.
JavaScript’s loose equality (==) allows type coercion, leading to surprising results.
Explanation:
● 0 and "" are coerced to false when compared with false using ==.
● Strict equality (===) checks both type and value.
Exercise:
console.log(null == undefined); // ?
console.log(null === undefined); // ?
Answer:
3. Global this
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
3
Example: Global this in Browsers
console.log(this); // Output: Window object
Explanation:
4. Floating-Point Math
JavaScript uses IEEE 754 for numbers, which can lead to precision issues.
Explanation:
● Floating-point numbers are represented in binary, which can’t exactly represent some
decimal values.
Fix:
console.log(Number((0.1 + 0.2).toFixed(2))); // Output: 0.3
Explanation:
4
Variables declared with var are function-scoped, not block-scoped.
Explanation:
● var is scoped to the enclosing function or global scope, not the block.
7. Immutability of const
Explanation:
● The reference to obj is immutable, but the object itself can be modified.
Output:
Exercises
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
5
Exercise 1: Predict typeof Output
console.log(typeof undefined); // ?
console.log(typeof NaN); // ?
console.log(typeof function() {}); // ?
Answer:
1. "undefined"
2. "number"
3. "function"
Solution:
console.log([] == false); // ?
console.log(![] == false); // ?
Answer:
Multiple-Choice Questions
Question 1:
console.log(typeof null);
1. "null"
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis
6
2. "object"
3. "undefined"
4. "function"
Answer: 2. "object"
Question 2:
1. ""
2. "{}"
3. "[object Object]"
4. 0
Question 3:
1. window
2. document
3. {} (empty object)
4. null
Answer: 1. window
Best Practices
Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis