typeof operators in JavaScript
In this article, you will learn the list of typeof operators in JavaScript. The typeof
operator is used to get the type of the provided operand as a string.
Checkout more articles on JavaScript
Syntax
The syntax for the typeof
operator.
1 | typeof operand |
operand: an expression whose type is to be returned.
List of the different JavaScript datatypes
1. String
1 2 3 | const str = "Clue Mediator"; console.log(typeof str); // Output: string |
2. Number
1 2 3 | const num = 5; console.log(typeof num); // Output: number |
3. BigInt
1 2 3 | const bigNum = 2084964762905899537925344n; console.log(typeof bigNum); // Output: bigint |
4. Boolean
1 2 3 | const x = true; console.log(typeof x); // Output: boolean |
5. Undefined
1 2 3 | const x = undefined; console.log(typeof x); // Output: undefined |
6. Object
1 2 3 | const obj = { }; console.log(typeof obj); // Output: object |
7. Array
1 2 3 | const arr = [ ]; console.log(typeof arr); // Output: object |
Note: The type of Array
is Object!
To detect if an expression is an array
, use the Array.isArray()
method.
1 2 | console.log(Array.isArray(arr)); // Output: true |
8. Function
1 2 3 | const fn = function () { return 'Im a function' }; console.log(typeof fn); // Output: function |
9. null
1 2 3 | const n = null; console.log(typeof n); // Output: object |
Note: The type of null
is Object!
I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂