A data type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data.
A value in JavaScript is always of a certain type. For example, a string or a number.
There are 8 basic data types in JavaScript.
Seven primitive data types:
number
for numbers of any kind: integer or floating-point, integers are limited by ±(253-1).bigint
for integer numbers of arbitrary length.string
for strings. A string may have zero or more characters, there’s no separate single-character type.boolean
for true/false.null
for unknown values – a standalone type that has a single value null.undefined
for unassigned values – a standalone type that has a single value undefined.symbol
for unique identifiers. (new to ES6)
And one non-primitive data type:
object
for more complex data structures.
We will go throught all of them in this article. Let’s step!
Table of contents
Open Table of contents
What is typeof
operator?
JavaScript provides a typeof
operator that can examine a value and tell you what type it is.
- Usually used as
typeof x
, buttypeof(x)
is also possible. - Returns a string with the name of the type, like “string”.
- For
null
returns “object” – this is an error in the language, it’s not actually an object
let transform;
typeof transform; // "undefined"
transform = "hello there";
typeof transform; // "string"
transform = 22;
typeof transform; // "number"
transform = false;
typeof transform; // "boolean"
transform = undefined;
typeof transform; // "undefined"
transform = null;
typeof transform; // "object" -- so confusing
transform = { clothes: "red" };
typeof transform; // "object"
transform = ["clothes", "hair", "materials"];
typeof transform; // "object"
transform = Symbol("Any thing here");
typeof transform; // "symbol"
What is the object
type?
The object
type refers to a compound value where you can set properties (named locations) that each hold their own values of any type.