JavaScript typeof


In JavaScript, there are five types of data that can store information.

  • string
  • number
  • boolean
  • object
  • function

There are 6 types of objects:

  • Object
  • Date
  • Array
  • String
  • Number
  • Boolean

Two data types that cannot hold values:

  • null
  • undefined

The typeof Operator

You can figure out the data type of a JavaScript variable by using the typeof operator.

Please observe:

  • The data type of NaN is number
  • The data type of an array is object
  • The data type of a date is object
  • The data type of null is object
  • The data type of an undefined variable is undefined *
  • The data type of a variable that has not been assigned a value is also undefined *

You cannot use typeof to determine if a JavaScript object is an array (or a date).


Primitive Data

A basic data value is a simple piece of information without any extra features or actions.

You can find out the type of a value using the typeof operator, which can give you one of these basic types:

  • string
  • number
  • boolean
  • undefined

Complex Data

You can use the typeof operator to get one of two complex types:

  • function
  • object

The typeof operator tells you that something is an object when you use it with objects, arrays, or null.

The typeof operator doesn't show object when used with functions.

The typeof tool shows that arrays are treated as objects in JavaScript, so when you use it with arrays, it returns object.


The Data Type of typeof

The typeof operator isn't a variable; it's an operator. Unlike variables, operators ( + - * / ) don't have a specific data type.

The typeof operator always gives a string, showing what type the thing is.


The constructor Property

The constructor property gives you the constructor function for all JavaScript variables.

You can use the constructor property to determine if an object is an array, meaning it has the word Array in it.

A simpler way is to verify whether the object is an Array by using the Array function.

To determine if an object is a Date (contains the word "Date"), you can examine the constructor property using the Date constructor.

Or even simpler, you can check if the object is a Date function:


Undefined

In JavaScript, when a variable doesn't have a value assigned to it, it is considered as having the value undefined. The type of such a variable is also undefined.

You can clear a variable by assigning the value undefined. The variable's type will then become undefined as well.


Empty Values

A blank value isn't connected to undefined.

A blank space can be considered both valid and categorized by its type.


Null

In JavaScript, when you see null, think of it as representing nothing. It's meant to signify the absence of something that should be there.

Unfortunately, in JavaScript, when you use the data type "null," it's treated as an object.

In JavaScript, there's a little mistake where typeof null says it's an object, but it should actually say null.

You can make an object empty by assigning it the value null:

In JavaScript, when you set an object to undefined, it means you're making it empty or indicating that it doesn't have a defined value.


Difference Between Undefined and Null

In JavaScript undefined and null have the same value, but they are of different types.


The instanceof Operator

The instanceof operator tells you if an object is an example of a particular type. It gives back true when the object is an instance of the specified type.


The void Operator

The void operator checks an expression and gives back undefined. It's commonly used to get the undefined basic value, like in "void(0)" (helpful when you want to evaluate an expression without using its return value).