Understanding JavaScript types
Before understanding the type system in TypeScript, let’s briefly explore the type system in JavaScript. To do this, open the CodeSandbox at https://codesandbox.io/ and carry out the following steps:
- Create a new plain JavaScript project by choosing the Vanilla option.
- Open
index.js
, remove its content, and replace it with the following code:let firstName = "Fred"
console.log("firstName", firstName, typeof firstName);
let score = 9
console.log("score", score, typeof score);
let date = new Date(2022, 10, 1);
console.log("date", date, typeof date);
The code assigns three variables to various values. The code also outputs the variable values to the console, along with their JavaScript type.
Here’s the console output:
Figure 2.5 – Some JavaScript types
It isn’t surprising that firstName
is a string and score
is a number. However, it is a little...