The typeof Operator

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — What is the typeof Operator?

When you're coding, sometimes you need to check what type of data you're working with. Is it a number? A string? A boolean? JavaScript gives us a special tool for this: the typeof operator!

The typeof operator tells you the type of any value. Here's how to use it:

// Basic syntax console.log(typeof 42); console.log(typeof "Hello"); console.log(typeof true); // You can also use parentheses console.log(typeof(42));

Notice that typeof always returns a string that describes the type. For example, typeof 42 returns the string "number", not the number itself!

Why do we need typeof?

JavaScript is a dynamically typed language, meaning variables can hold different types of values. The typeof operator helps you check what type you're working with before performing operations. This is especially useful when you receive data from users or other sources and need to validate it!

Exercise 2 — typeof with Different Types

Let's explore what typeof returns for each primitive type you've learned! Try running this code and experiment by changing the values:

let codemonName = "Aquachu"; let codemonLevel = 25; let isWaterType = true; let specialMove; console.log("Name type:", typeof codemonName); console.log("Level type:", typeof codemonLevel); console.log("Water type check:", typeof isWaterType); console.log("Special move type:", typeof specialMove);

Here's what typeof returns for each type: - "string" for text values - "number" for numeric values (integers and decimals) - "boolean" for true/false values - "undefined" for variables that haven't been assigned a value

Exercise 3 — Predicting typeof Results

What will the following code output?

let codemonHP = 100; console.log(typeof codemonHP);
Options
• "number"
• boolean
• "integer"
• 100

Hint : Remember: typeof always returns a string that describes the type. The value 100 is a numeric value.

Exercise 4 — Using typeof Operator

Complete the code to check the type of the codemonName variable and log it to the console:

Hint : The typeof operator is followed by the variable name (without quotes). Use console.log() to display the result.

Exercise 5 — Match Values to Their typeof Results

Match each value with what typeof would return for it:

Items to match
• "Florachu"
• 42.5
• false
• undefined
• 0
Definitions
• "string"
• "number"
• "boolean"
• "undefined"
• "number"

Exercise 6 — Understanding typeof Return Value

What is the type of the value that typeof returns?

let result = typeof 42; console.log(typeof result);
Options
• "string"
• "number"
• "type"
• "undefined"

Hint : The typeof operator always returns a string that describes the type. Even when you check the type of the result, it's a string!

Exercise 7 — Display Variable Types

Practice using typeof to check variable types! Your task: 1. Create a variable codemonName with a string value 2. Create a variable codemonLevel with a number value 3. Create a variable isCodemonAlive with a boolean value 4. Use typeof and console.log() to display the type of each variable

Starter code
// Create your variables and display their types here
Back to the course