JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
8: Primitive Types Explained
I want to show you a snippet of code that I actually saw in a pull request a few months ago. The developer was trying to build a simple configuration handler for a notification system. They wanted to check if a user had specified a custom "alert threshold" and, if not, fall back to a default value of 10. Here is what they wrote:
const userConfig = {
alertThreshold: 0 // The user explicitly wants 0 alerts
};
const threshold = userConfig.alertThreshold || 10;
console.log(`Alerting at: ${threshold}`);
// Expected: "Alerting at: 0"
// Actual: "Alerting at: 10"
The developer was baffled. They set the value to 0, but the code insisted on using 10. They thought there was a bug in the logical OR operator, but the real issue is that they didn't fully grasp how JavaScript treats different primitive types in a boolean context.
The "Falsy" Confusion
In JavaScript, every single value has an inherent boolean truthiness. When you use the || operator, JavaScript doesn't just look for "existence"; it looks for a "truthy" value. The number 0 is a primitive of the number type, but it is considered falsy. So, JavaScript sees 0, decides it's "false," and skips right over to the fallback value of 10.
To fix this, we need to be explicit about what we are looking for. We aren't looking for "truthiness"; we're looking for whether the value is undefined or null. The modern way to handle this is the nullish coalescing operator (??):
const threshold = userConfig.alertThreshold ?? 10;
// Now, if alertThreshold is 0, it stays 0.
// If it's null or undefined, it becomes 10.
The Seven Primitives
To avoid these kinds of head-scratchers, you have to internalize what a "primitive" actually is. In JavaScript, a primitive is data that is not an object and has no methods. They are the simplest building blocks of the language. There are seven of them:
- Number: Handles both integers and floats (like
42or3.14). - String: Textual data (like
"Hello"). - Boolean: Either
trueorfalse. - Undefined: A variable that has been declared but not yet assigned a value.
- Null: An intentional absence of any object value. (Yes, it's a primitive, even though
typeof nullweirdly returns"object"—that's a legacy bug from the first version of JS that we just live with now). - BigInt: For integers too large to be represented by the standard Number type.
- Symbol: Used for creating unique identifiers for object properties.
Primitives are Immutable
Here is where a lot of people get tripped up when they move from primitives to objects. Primitives are immutable. This doesn't mean the variable can't change, but the value itself cannot be altered.
Take a look at this. You might think you can change a single character in a string like you can in some other languages:
let name = "Alice";
name[0] = "B";
console.log(name); // Still "Alice"
JavaScript doesn't throw an error; it just silently fails. You can't "mutate" the string "Alice" into "Blice." To change the value, you have to replace the entire primitive with a new one: name = "Blice";. I like to think of primitives as read-only values. If you want a different value, you just toss the old one away and grab a new one.
📋 Practical Task
The Strict Configuration Validator
You are building a settings module for a video game. You need to create a function called validateSetting that takes a currentValue and a defaultValue.
The logic should be:
- If the
currentValueisnullorundefined, return thedefaultValue. - If the
currentValueis any other primitive (including0,false, or an empty string""), return thecurrentValue.
Test your function with these cases:
validateSetting(0, 20); // Should return 0
validateSetting(undefined, 20); // Should return 20
validateSetting(null, 20); // Should return 20
validateSetting(false, true); // Should return false
validateSetting("", "Default"); // Should return ""
There are no comments for now.