Documentation

Student-friendly HTML, CSS, and JavaScript reference with examples

Sections

Type Checking with typeof

The typeof operator returns the type of a value as a string.

typeof 5           // 'number'
typeof 'hello'     // 'string'
typeof true        // 'boolean'
typeof undefined   // 'undefined'

Comparing Types

You can check if two values have the same type:

typeof input1 === typeof input2

Examples

const value1 = 5;
const value2 = '5';

typeof value1            // 'number'
typeof value2            // 'string'
typeof value1 === typeof value2  // false

Use Cases

const sameType = typeof input1 === typeof input2;
const isNumber = typeof age === 'number';
const isText = typeof name === 'string';