Last updated on September 18, 2023 by Siraj Ul Haq
typeof:The typeof keyword represents the operator.
value_or_variable:It is the value or variable whose data type needs to check.
Look at the below examples in which we are going to check the data type of different values.
Example 1: Check String Data Type
let customerName = "Henry"; console.log(typeof customerName); // output: string
In this example, we assigned a string value containing the customer’s name “Henry” to the variable “customerName”. To check the datatype of the customerName variable, we are using typeof operator along with the console.log() function that returns “string” in the output which means the data type of the customerName variable is a string.
Example 2: Check Number Data Type
let productQuantity = 35; console.log(typeof productQuantity); // output: number
Example 3: Check Data Type of a Numeric String
let orderNumber = "40"; console.log(typeof orderNumber); // output: string
In this example, we assigned a numeric string “40” to the variable orderNumber. Since it has double quotes it is treated as a string. As a result, the “typeof” operator returns “string” as the data type, and “console.log()” displays “string” as the output.