Types

In this lesson, we will explore JavaScript types. Let's begin!

Values and variables always have a concrete type that you can access with the typeof operator, as shown is this sample:

Press + to interact
var myValue = "this is a value";
console.log(typeof new Object());
console.log(typeof myValue);
console.log(typeof (42));

JavaScript has only seven types by means of the value domain of the typeof operator. They are:

  1. object
  2. function
  3. string
  4. number
  5. boolean
  6. null
  7. undefined

The value of string, number, or boolean types are retrieved by typeof, respectively.

If the value is an object, or null (this value represents an empty object pointer), typeof returns object.

As you already learned, functions are first class citizens in JavaScript, so typeof retrieves function if you apply it on a function definition, as shown in this code snippet:

Press + to interact
console.log(typeof fortytwo);
function fortytwo() {
return 42;
}

The undefined type has only one special value, undefined. When a variable is declared but not initialized, or you refer to a non-existing property of an object value, ...