typeof operators in JavaScript
π
February 8, 2022
π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
- array-in-javascript" title="How to search an array in JavaScript">How to search an array in JavaScript
- How to generate a Unique ID in JavaScript
- scroll-to-a-specific-element-using-javascript" title="Scroll to a specific element using JavaScript">Scroll to a specific element using JavaScript
- Detect a mobile device using JavaScript
Syntax
The syntax for the `typeof` operator.
typeof operand
operand: an expression whose type is to be returned.
List of the different JavaScript datatypes
- String
- number" title="Number">Number
- BigInt
- boolean" title="Boolean">Boolean
- Undefined
- Object
- Array
- Function
- null
1. String
const str = "Clue Mediator";
console.log(typeof str);
// Output: string
2. Number
const num = 5;
console.log(typeof num);
// Output: number
3. BigInt
const bigNum = 2084964762905899537925344n;
console.log(typeof bigNum);
// Output: bigint
4. Boolean
const x = true;
console.log(typeof x);
// Output: boolean
5. Undefined
const x = undefined;
console.log(typeof x);
// Output: undefined
6. Object
const obj = { };
console.log(typeof obj);
// Output: object
7. Array
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.
console.log(Array.isArray(arr));
// Output: true
8. Function
const fn = function () { return 'Im a function' };
console.log(typeof fn);
// Output: function
9. null
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..!! π