Hello! Welcome back to our module on JavaScript Fundamentals.
In our last lesson, we learned how to create "containers" for data using let and const. You now know how to declare variables and the crucial difference between values that can change (let) and those that should remain constant (const).
Today, we'll look at what we can put inside those containers. The goal of this lesson is to learn how to work with primitive data types including strings for text content, numbers for quantities, booleans for toggles, null and undefined for missing values.
These data types are the fundamental building blocks of information in JavaScript. As a designer, you already work with these concepts intuitively: every piece of text, every price, and every toggle switch on a screen corresponds directly to one of these data types.
1. The Building Blocks: Primitive Data Types
In JavaScript, data is categorized into different types. The most basic category is primitive types. These are the simplest, most fundamental pieces of data—they are immutable, meaning they can't be broken down into smaller parts.
Let's start with a video that introduces the concept of data types and covers the three most common primitives you'll use every day: strings, numbers, and booleans.
JavaScript Data Types - Understanding Primitive Values
This video from the Future Fullstack channel uses a great juice bar analogy to explain what data types are. It then provides clear definitions and real-world examples of numbers, strings, and booleans, connecting them to elements you'd see on websites like YouTube and EasyJet.
Please watch the following segments. Pay close attention to how each data type is represented in the code and how it maps to a real UI element. Introduction to Data Types (00:12 - 02:34): Understand the difference between primitive and complex values. Overview and Numbers (02:34 - 03:27): Learn about the number type for quantities and calculations. Strings (03:27 - 04:01): See how the string type is used for all text content. Booleans (04:01 - 05:24): Grasp the concept of boolean values (true/false) for decision-making. Real-World Examples (07:15 - 09:48): This section is particularly relevant as it shows these data types in action on the YouTube and EasyJet websites.
2. The Three Core Primitives
As you saw in the video, the three most common primitive types are:
-
String: Used for text. Any sequence of characters (letters, numbers, symbols) enclosed in single (
' ') or double (" ") quotes is a string.- Design Connection: Page titles, button labels, user comments, product descriptions.
const pageTitle = "User Settings";let buttonText = 'Submit';
-
Number: Used for all numeric values, including integers and decimals.
- Design Connection: The number of items in a cart, the price of a product, a user's age.
const itemsInCart = 3;let price = 19.99;
-
Boolean: Represents a logical state and can only have one of two values:
trueorfalse. Note that these are special keywords, not strings.- Design Connection: The state of a toggle switch (e.g., dark mode on/off), a checkbox (
checkedorunchecked), or whether a user is logged in. const isLoggedIn = true;let darkModeEnabled = false;
- Design Connection: The state of a toggle switch (e.g., dark mode on/off), a checkbox (
3. Representing "Nothing": null and undefined
Besides having a value like text or a number, a variable can also represent the absence of a value. JavaScript has two special primitive types for this: null and undefined. The distinction between them is a common source of confusion for newcomers, so let's clarify it.
undefinedtypically means a value has not been assigned yet. It's often an unintentional or temporary state. For example, if you declare a variable withletbut don't give it a value, its value isundefined.nullis an intentional absence of a value. As the programmer, you would assignnullto a variable to explicitly state that it has no value.
This next video explains the difference with a fantastic, practical example of an optional form field—a scenario you've likely designed many times.
JavaScript Null & Undefined – Explained Like Never Before
This follow-up video from Future Fullstack focuses exclusively on null and undefined. It clearly explains the purpose of each and when you would encounter them.
Watch the video to understand the conceptual and practical differences. Focus on: The definitions of null and undefined (00:11 - 01:37). The detailed comparison table and explanation (01:37 - 04:41). The real-world example of an optional address field, which is a perfect use case for null (04:29 - 05:47). The final summary and code example (05:36 - 09:05).
To summarize the video's key point:
let userSelection;//userSelectionisundefinedbecause no value was assigned.const middleName = null;//middleNameisnullbecause the user intentionally has no middle name.
4. Reinforcing Your Knowledge
The videos provide a great visual and conceptual overview. Now, let's round out your understanding with some reading. The MDN (Mozilla Developer Network) docs are the official reference for web technologies, and it's good practice to get familiar with them.
JavaScript data types and data structures - MDN Web Docs
This MDN article is the definitive guide to JavaScript data types. We'll read a few key sections to formalize what you've learned from the videos.
Please read the following sections. Don't worry about memorizing everything; focus on the main definitions. Start with 'Dynamic and weak typing' to understand that a variable's type can change. Read the introduction to 'Primitive values' and look at the summary table. Skim the short sections for 'Null type', 'Undefined type', 'Boolean type', 'Number type', and 'String type'. You can ignore BigInt and Symbol for now, as they are for more advanced use cases.
One useful tool mentioned in the documentation is the typeof operator. It allows you to check the data type of a variable in your code, which is incredibly helpful for debugging.
const name = "Jane";
console.log(typeof name); // Outputs: "string"
const age = 30;
console.log(typeof age); // Outputs: "number"
const isDesigner = true;
console.log(typeof isDesigner); // Outputs: "boolean"
let currentProject;
console.log(typeof currentProject); // Outputs: "undefined"
const noMiddleName = null;
console.log(typeof noMiddleName); // Outputs: "object" (This is a famous, long-standing bug in JS!)
Your Turn: Apply the Concepts
Let's put this into practice. Imagine you are working on the design for a user profile page. Below is a list of data points for a user. In your code editor (or even just in your head), decide which primitive data type would be most appropriate for each, and declare it as a variable using let or const.
- The user's full name: "Amelia Chen"
- The user's age: 28
- Whether the user is a premium subscriber: Yes
- The user's profile picture URL: "https://example.com/images/amelia.jpg"
- The number of projects the user has created: 15
- The user's optional company name: (they left it blank)
Click here to see how you might code this.
// 1. Full name is text -> string
const fullName = "Amelia Chen";
// 2. Age is a quantity -> number
const age = 28;
// 3. Premium status is a toggle -> boolean
const isPremiumSubscriber = true;
// 4. A URL is text -> string
const profilePictureUrl = "https://example.com/images/amelia.jpg";
// 5. Number of projects can change -> number, using let
let projectCount = 15;
// 6. Optional field left blank is an intentional absence of value -> null
const companyName = null;
Conclusion
Great work! You now understand the fundamental data types that form the basis of all information in JavaScript.
Key Takeaways:
- Primitive types are the basic building blocks of data:
string,number,boolean,null, andundefined. - Strings are for text, enclosed in quotes.
- Numbers are for numerical quantities, both integers and decimals.
- Booleans (
true/false) are for logical states, like toggles. undefinedmeans a value hasn't been assigned.nullmeans a value is intentionally absent.
So far, we've learned how to create variables and what types of data to put in them. In our next lesson, we'll learn how to work with this data using operators. This will allow us to perform calculations (like price * quantity), make comparisons (is itemsInCart > 0?), and build the logic that makes an application interactive.
Can't find a good explanation? Sign up and we'll make it for you
Sign up