The typeof operator in JavaScript is used to determine the data type of its operand. It returns a string representing the data type. The operand can be any object, function, or variable.
Syntax:
typeof operand
OR
typeof (operand)
Operand is an expression representing the object or primitive whose type is to be returned. An operand is essentially the thing that an operator operates on. In the context of the typeof
operator in JavaScript, the operand is the variable, expression, function, or object for which you want to determine the data type.
The possible types that exist in javascript are:
- undefined
- Object
- boolean
- number
- string
- symbol
- function
Here’s a simple example:
let myVar = 42; console.log(typeof myVar); // Outputs: "number" let myString = "Hello, ChatGPT!"; console.log(typeof myString); // Outputs: "string" let myBool = true; console.log(typeof myBool); // Outputs: "boolean" let myArray = [1, 2, 3]; console.log(typeof myArray); // Outputs: "object" let myFunction = function() { console.log("I'm a function!"); }; console.log(typeof myFunction); // Outputs: "function"