×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

JavaScript typeof Operator (With Example)

JavaScript typeof Operator: In this tutorial, we will learn about the typeof operator in JavaScript with the help of examples. By IncludeHelp Last updated : July 29, 2023

JavaScript typeof Operator

In JavaScript, the typeof operator is used to get the type of the data/value, variable, or expression. The typeof operator accepts an operand and returns the type of it.

Syntax

Below is the syntax of JavaScript typeof operator:

typeof operand;
or
typeof (operand);

Parameter

  • operand - It can be a value, variable/constant name, or an expression whose type you need to find.

Possible return values of typeof

The following are the possible return value of the JavaScript typeof operator (Reference):

TypeResult
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
BigInt "bigint"
String "string"
Symbol "symbol"
Function "function"
Any other object "object"

Example

This JavaScript example demonstrates the example of typeof operator. In this example, we are getting and printing the types of different types of variables and variables.

<html>
  <head>
    <title>JavaScipt Example</title>
  </head>
  <body>
    <script>		
      var a = 10;
      var b = 10.23;
      var c = "Hello";
      var d; 
      var e = "10+20";
      
      //printing type of variables
      document.write("type of a: " + typeof a + "<br>");
      document.write("type of b: " + typeof b + "<br>");
      document.write("type of c: " + typeof c + "<br>");
      document.write("type of d: " + typeof d + "<br>");
      document.write("type of e: " + typeof e + "<br>");
      
      //some of the expressions
      document.write(typeof (a+b) + "<br>");
      document.write(typeof (a+b+c) + "<br>");
      document.write(typeof (a+b+c+d) + "<br>");
      document.write(typeof (a+b+c+e) + "<br>");
      document.write(typeof (10+20+1.23) + "<br>");
      document.write(typeof (NaN) + "<br>");
    </script>
  </body>
</html>

Output

The output of the above example is:

type of a: number
type of b: number
type of c: string
type of d: undefined
type of e: string
number
string
string
string
number
number

JavaScript Tutorial »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.