JavaScript: typeof operator
JavaScript Type Checking
The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. The operator returns the data type.
Syntax
typeof operand or typeof (operand)
There are six possible values that typeof returns: object, boolean, function, number, string, and undefined. The following table summarizes possible values returned by the typeof operator.
Type of the operand | Result |
---|---|
Object | "object" |
boolean | "boolean" |
function | "function" |
number | "number" |
string | "string" |
undefined | "undefined" |
Examples of typeof operator: string
Console
> typeof "" "string" > typeof "abc" "string" > typeof (typeof 1) "string"
Examples of typeof operator: number
Console
> typeof 17 "number" > typeof -14.56 "number" > typeof 4E-3 "number" > typeof Infinity "number" > typeof Math.LN2 "number" > typeof NaN "number"
Examples of typeof operator: boolean
Console
> typeof false "boolean" > typeof true "boolean"
Examples of typeof operator: function
Console
> typeof Math.tan "function" >typeof function(){} "function"
Examples of typeof operator: object
Console
> typeof {a:1} "object" > typeof new Date() "object" > typeof null "object" > typeof /a-z/ "object" > typeof Math "object" > typeof JSON "object"
Examples of typeof operator: undefined
Console
> typeof undefined "undefined" > typeof abc "undefined"
More examples on typeof operator
typeof(4+7); //returns number
typeof("4"+"7"); //returns string
typeof(4*"7"); //returns number
typeof(4+"7"); //returns string
What is the difference between typeof myvar and typeof(myvar) in JavaScript?
There is absolutely no difference between typeof myvar and typeof(myvar). The output of the following codes will be same i.e. "undefined".
alert(typeof myvar);
alert(typeof(myvar));
How to detect an undefined object property in JavaScript?
The following method is the best way to detect an undefined object property in JavaScript.
if (typeof xyz === "undefined")
alert("xyz is undefined");
How to check whether a variable is defined in JavaScript or not?
The following method is the best way to detect an undefined object property in JavaScript.
if (typeof xyz === "undefined") alert("xyz is undefined"); if (typeof xyz === "undefined") alert("Varaible is undefined"); else alert("Variable is defined");
The following example tests the data type of variables.
JS Code
var index = 8;
var result = (typeof index === 'number');
alert(result);
// Output: true
var description = "w3resource";
var result = (typeof description === 'string');
alert(result);
// Output: true
Previous: JavaScript: this Operator
Next: JavaScript: void operator
Test your Programming skills with w3resource's quiz.
JavaScript: Tips of the Day
Property name
const { name: myName } = { name: 'Owen' }; console.log(name);
When we unpack the property name from the object on the right-hand side, we assign its value "Owen" to a variable with the name myName.
With { name: myName }, we tell JavaScript that we want to create a new variable called myName with the value of the name property on the right-hand side.
Since we try to log name, a variable that is not defined, a ReferenceError gets thrown.
Ref: https://bit.ly/3jFRBje
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook