Code highlights logo

Learn

Variables

typeof operator

The typeof operator is used to determine the type of a value or variable. It returns a string indicating the type of the operand.

Syntax

The syntax of the typeof operator is as follows:

1typeof operand

Usage

The typeof operator can be used with different types of operands.

Here are some examples:

  1. Checking the type of a variable:
    1let username = "John";
    2console.log(typeof username); // Output: "string"
  2. Checking the type of a numeric value:
    1console.log(typeof 42); // Output: "number"
  3. Checking the type of a boolean expression:
    1let isActive = true;
    2console.log(typeof true); // Output: "boolean"
  4. Checking the type of an undefined value:
    1let age;
    2console.log(typeof age); // Output: "undefined"

The typeof operator is a useful tool in JavaScript for determining the type of a value or variable. It returns a string representing the data type of the operand.

Understanding the type of data can help in writing more robust and error-free code.


Instructions

1.

Declare a variable named result1 and assign it the result of applying the typeof operator to the variable num.

2.

Print the value of the result1 variable to the console.

Sign up to start coding

Already have an account?

Sign In

Course content