Clue Mediator

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

Syntax

The syntax for the `typeof` operator.

typeof operand

operand: an expression whose type is to be returned.

List of the different JavaScript datatypes

  1. String
  2. number" title="Number">Number
  3. BigInt
  4. boolean" title="Boolean">Boolean
  5. Undefined
  6. Object
  7. Array
  8. Function
  9. 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..!! πŸ™‚