JavaScript typeof Operator

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Syntax of typeof in JavaScript

The syntax of the typeof in javascript can be of two forms:

and

Parameters of typeof in JavaScript

The parameter of the typeof in the javascript operator is as follows:

  • operand: the operand represents the object whose type we want to be returned.

operand

Operands can be defined as the result of an operator that may be a constant, variable, or function too.

  • The possible types that exist in javascript are:
  1. undefined: the undefined indicates that the variables have not been assigned a value. Example:
  1. Object:

Output:

  1. boolean: The boolean is a logical data type that returns only two values that is true or false for a given expression.

Output:

  1. number: It is a numeric data type like integer, float, and doubles.

Output:

  1. string: String refers to a sequence of characters that represents a text.

Output:

  1. symbol: A symbol is a built-in object. The constructor of symbol returns a symbol primitive or a symbol value.

Output:

  1. function: functions are a particular set of codes that are used to perform a simple, small task that is a part of the whole program. We can call a function in a program as many times as we want in typeof in javascript.

Output:

How Do You Use the Javascript TypeOf Operator?

As we know, the typeof in javascript operator returns the data type of that variable where we use it for. Let us see an example to understand how the typeof operator works in JavaScript:

Output:

Explanation: In the above example, two variables of the int type and string type are declared. Then a line break is created using the <br> tag. Then result is initialized which checks the type of data type of int x and string y that is then assigned to the variable result. The typeof in javascript operator is used in this way.

Examples of typeof in JavaScript

Let us see some more examples to understand the typeof operator in JavaScript.

Basic Usage

This basically means checking all the data types present in a code. Let us have a look at the given example of typeof in javascript where we used all kinds of data types like number, string, boolean, etc returned.

Output:

Explanation: In the above example of typeof in javascript, we used document.write to print the type of data types. Here three data types are used that is string, number, and undefined. In the parentheses of document.write, we provided the additional information that is of whose data type we want to get.

typeof Null

Explanation": Here the type tag is 0 of the object. And null is presented as the NULL pointer. Since the type tag of null is 0 hence, object is returned as the return value of typeof.

Using new Operator

If a built-in function has a constructor function, then the new operator can be used to create a user-defined object type in typeof in javascript. Let us see an example.

Explanation: In the first line, we are initializing a string and storing it in the object strg. In the next line, we are creating a Number object and store the result in the number variable.

At last, we are initializing a function and store the result in the func variable.

The Need for Parentheses in Syntax

We use parentheses in a function for providing a piece of additional information. The typeof operator in JavaScript has more priority as compared to a binary operator like addition (+). So, parentheses are required to evaluate the type of an addition result in typeof in javascript. The parentheses can be also used to determine the data type of a given expression.

Example:

Regular Expressions

The regular expressions of typeof operator in JavaScript are a kind of pattern that be used to match the character combinations of a string. The regular scripts are also an object.

Example:

Errors

Before the major revision to JavaScript, ECMAScript 2015, the typeof operator needed to return some string value for any operand it was given.

Even if the identifiers are not declared, the typeof operator must return a value 'undefined'. So, the typeof operator never produces an error.

But after the ECMAScript 2015, some changes are implemented for the typeof operator. After that whenever we use the typeof operator on let and const, even if they are not declared, it will throw a Reference error.

Example:

Exceptions

Almost all the current browsers show a non-standard host object named document.all with the type of undefined.

Real-world Usage

We can see the real-world use of the typeof operator in websites that uses password concepts. Suppose a website has a login portal, where we must need to enter the password for logging in. So, the system checks are the password is secure or not. For this, we can use the typeof operator to check if there is any string present in the password or not.

Browser Compatibility

The typeof operator in javascript is supported by all the popular browsers.

browser-compatibility-of-typeof-operator

Conclusion

  • The typeof operator in JavaScript is used to check the type of data type.
  • It checks the data type of operand that it has been given.
  • typeof operator can be written in the following two ways:-
    1. typeof operand
    2. typeof (operand)
  • Before ECMAScript 2015, the typeof operator never gives an error, even for undefined variables it shows undefined.
  • The parentheses are used to provide extra information to the typeof operator javascript if the user wants to.