Get Type Of Variable in JavaScript

Get Type Of Variable in JavaScript | Here we will discuss how to check a variable or object’s type in JavaScript. The typeof operator in JavaScript is used to identify the type of an object or variable. JavaScript, on the other hand, uses dynamic typing, sometimes known as weak typing. This shows that a variable is capable of taking on any kind of value. A variable’s type is determined by the type of value it has been given.

You may identify the kind of value or type of value that a variable contains in JavaScript by using the typeof operator. The typeof operator, a unary operator with a single operand, accepts a single variable as input. A string is returned after it determines the type of the operand. Also see:- JavaScript Replace Last Occurrence

What is returned by the typeof operator?
The typeof operator will return one of the data types listed below if we use it with any primitive data type:

  • String.
  • Boolean (for true or false) (for true or false).
  • Number (for int, float, etc). (for int, float, etc.).
  • If a variable has no value, it is undefined.

If we look at the data types of the methods, objects, and arrays, the typeof operator will return one of the data types listed below:-

  • function (for methods) (for methods).
  • object (for objects and arrays because in JavaScript, arrays are viewed as objects) (for objects and arrays because in JavaScript, arrays are considered as objects).

Examples to Get Type Of Variable in JavaScript

Let’s comprehend the nature of typeof operator in JavaScript using some illustrations:-

Example 1: The output of a typeof check on a string variable is “string.”

var KP = "KnowProgram";
console.log(typeof KP);

Output:-

string

Example 2: The string “number” is returned when the type of a number variable is checked.

var number = 87455498;
console.log(typeof number);

Output:-

number

Example 3: “undefined” is the type of undefined when we check it.

console.log(typeof undefined);

Output:-

undefined

Example 4: “String” is the special symbol’s type.

var symbol = "$";
console.log(typeof symbol);

Output:-

string

Example 5: “Object” is the type of null. Let us see the program for javascript get type of variable when it is null.

console.log(typeof null);

Output:-

object

Example 6: “number” is returned when the type is NaN (not a number).

console.log(typeof NaN);

Output:-

number

Example 7: In this instance, a new keyword is used to create a new object. typeof the newly formed variable is “object.”

let obj = new String("This is a string")
console.log(typeof obj);

Output:-

object

Example 8: In the example that follows, a function that calculates the sum of two numbers is built and then provided to a variable. The function variable’s type is “function.”

let func_object = function (x, y) {
    return x + y;
};

console.log(typeof func_object);

Output:-

function

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *