feat(curriculum): Add interactive examples to How Does the typeof Operator Work, and What Is the typeof null Bug in JavaScript (#63186)

This commit is contained in:
Clarence Bakosi
2025-10-28 23:02:38 +01:00
committed by GitHub
parent 9513e5d878
commit 64a3e5af19
@@ -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.