Skip to Content
Course content

8: Primitive Types Explained

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 42 or 3.14).
  • String: Textual data (like "Hello").
  • Boolean: Either true or false.
  • 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 null weirdly 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 currentValue is null or undefined, return the defaultValue.
  • If the currentValue is any other primitive (including 0, false, or an empty string ""), return the currentValue.

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 ""
Rating
0 0

There are no comments for now.

to be the first to leave a comment.