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:
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:
- object
- function
- string
- number
- boolean
- null
- 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:
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, ...