From 64a3e5af19766a727a93b668e9a61da64306165a Mon Sep 17 00:00:00 2001 From: Clarence Bakosi Date: Tue, 28 Oct 2025 23:02:38 +0100 Subject: [PATCH] feat(curriculum): Add interactive examples to How Does the typeof Operator Work, and What Is the typeof null Bug in JavaScript (#63186) --- .../6732518a8627876f4fcd18a4.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/curriculum/challenges/english/blocks/lecture-working-with-data-types/6732518a8627876f4fcd18a4.md b/curriculum/challenges/english/blocks/lecture-working-with-data-types/6732518a8627876f4fcd18a4.md index b9a968c764f..85fdace6e6b 100644 --- a/curriculum/challenges/english/blocks/lecture-working-with-data-types/6732518a8627876f4fcd18a4.md +++ b/curriculum/challenges/english/blocks/lecture-working-with-data-types/6732518a8627876f4fcd18a4.md @@ -5,26 +5,34 @@ challengeType: 19 dashedName: how-does-the-typeof-operator-work-and-what-is-the-typeof-null-bug-in-javascript --- -# --description-- +# --interactive-- The `typeof` operator in JavaScript is a simple yet powerful tool that lets you see the data type of a variable or value. It always returns a string indicating the type. Let's take a look at a few examples: +:::interactive_editor + ```js let num = 42; console.log(typeof num); // "number" ``` +::: + In this first example, we have created a variable called `num` and assigned it the number `42`. When you use the `typeof` operator on the variable named `num`, it will return the string `number`. Here is another example of using the `typeof` operator on variable called `isUserLoggedIn`: +:::interactive_editor + ```js let isUserLoggedIn = true; console.log(typeof isUserLoggedIn); // "boolean" ``` +::: + When you use the `typeof` operator on the `isUserLoggedIn` variable, it will return a string `boolean` because the boolean `true` was assigned to the variable. Using the `typeof` operator can be especially useful when you're debugging or trying to understand what kind of data you're working with in your code. @@ -33,11 +41,15 @@ However, there's a well-known quirk in JavaScript when it comes to `null`. Let's take a look at an example: +:::interactive_editor + ```js let exampleVariable = null; console.log(typeof exampleVariable); // "object" ``` +::: + In this example, we have a variable called `exampleVariable` and have assigned it the value of `null`. But when we use the `typeof` operator, it returns the string `object`. This is widely considered a bug in JavaScript, dating back to its early days. The reason for this behavior is rooted in the way JavaScript was originally designed.