JavaScript Type Operators

JavaScript Type Operators are used to check the type of a variable or object, and to manipulate or determine their behavior based on their type.

List of Type Operators:

Operator Description Example Result
typeof Returns the type of a variable or expression typeof 42 "number"
instanceof Tests if an object is an instance of a class or constructor obj instanceof Array true/false
constructor Returns the constructor function for an object obj.constructor Function

1. typeof Operator

The typeof operator returns a string indicating the type of the operand. It is useful for determining the data type of variables and expressions.

Syntax:

typeof operand

Try It Now

Example:

console.log(typeof 42);             // "number"
console.log(typeof "Hello");        // "string"
console.log(typeof true);           // "boolean"
console.log(typeof undefined);      // "undefined"
console.log(typeof null);           // "object" (special case)
console.log(typeof {});             // "object"
console.log(typeof function(){});   // "function"

Try It Now

Special Cases:

  • typeof null returns "object" due to historical reasons in JavaScript.
  • typeof NaN returns "number" even though it stands for “Not-a-Number”.

2. instanceof Operator

The instanceof operator tests whether the prototype property of a constructor appears in the prototype chain of an object.

Syntax:

object instanceof constructor

Try It Now

Example:

let arr = [1, 2, 3];
console.log(arr instanceof Array);   // true
console.log(arr instanceof Object);  // true
console.log(arr instanceof String);  // false

Try It Now

3. constructor Property

The constructor property returns the constructor function that created the instance object.

Syntax:

object.constructor

Try It Now

Example:

let str = "Hello";
console.log(str.constructor); // function String() { [native code] }

let num = 42;
console.log(num.constructor);  // function Number() { [native code] }

let obj = {};
console.log(obj.constructor);  // function Object() { [native code] }

Try It Now

Using Type Operators:

Example: Checking Variable Type

function checkType(variable) {
    if (typeof variable === "string") {
        console.log("It's a string!");
    } else if (typeof variable === "number") {
        console.log("It's a number!");
    } else {
        console.log("It's something else!");
    }
}

checkType(42);       // "It's a number!"
checkType("Hello");  // "It's a string!"
checkType(true);     // "It's something else!"

Try It Now

Example: Checking Instances

class Animal {}
class Dog extends Animal {}

let myDog = new Dog();

console.log(myDog instanceof Dog);    // true
console.log(myDog instanceof Animal); // true
console.log(myDog instanceof Object); // true
console.log(myDog instanceof Array);  // false

Try It Now

Summary:

  • typeof: Useful for checking the data type of a variable.
  • instanceof: Checks if an object is an instance of a specific class or constructor.
  • constructor: Retrieves the constructor function of an object.

These type operators are essential for handling different data types and ensuring type safety in JavaScript.