Understanding the types in JavaScript
TypeScript adds a rich type system to JavaScript. Before we start to use this rich type system, we need to learn what types we have in JavaScript.
Primitive types in JavaScript #
JavaScript does have some basic primitive types, but what are they?
The JavaScript code below outputs the types of four variables with various kinds of values. We’d hope for string
, number
, boolean
, and date
to be output … but is this the case? Run the code and find out.
const name = "Bob";console.log("Type of name:", typeof name);const age = 30;console.log("Type of age:", typeof age);const cool = true;console.log("Type of cool:", typeof cool);const dateOfBirth = new Date(1989, 10, 5);console.log("Type of dateOfBirth:", typeof dateOfBirth);
Nearly! JavaScript does ...