Start learning free

LESSON 10 OF 414 · VARIABLES

Data Types in TypeScript

Lesson

TL ; DR:

In TypeScript, not all data is treated the same. There are different data types, such as:

  • number - represents numbers like 3 and 3.14

  • string - represents a string (sequence of characters) like "John Doe"

  • boolean - represents data that can have one of two values: true or false

  • null - represents a null value, or no value at all



Full lesson:

We've talked about data and variables, which are containers to store that data. But not all data is treated the same.

For example, TypeScript distinguishes between numbers, such as 12, and strings, such as "12" or "dog", which are collections of characters.

The most basic and used data types in TypeScript are:

  • number - represents integer numbers like 3 and -7 and floating point numbers like 3.14
  • string - represents a string (sequence of characters) like "John Doe" or 'dog'
  • boolean - represents a Boolean, data that can have one of two values: true or false
  • null - represents a null value, or no value at all

typeof operator

Every value has a type at run time. We can ask what type something is by using the typeof operator:

let x = 1;
console.log(typeof x); // Output: number

let y = 3.14;
console.log(typeof y); // Output: number

console.log(typeof "butter"); // Output: string

let z = true;
console.log(typeof z); // Output: boolean

Choosing what operation to perform:

One instruction can have several distinct meanings depending on the data types it's working with.

For example, suppose you see this line of code somewhere in a program:

console.log(a + b);

Can you tell for sure what it does without knowing anything about the variables a and b?

It might add two numbers, if a and b are both numbers:

let a = 10, b = -5;

console.log(a + b); // Output: 5

But it also might concatenate two strings, if a and b are both strings:

let a = "Hello ", b = "world!";

console.log(a + b); // Output: Hello world!

Of course, addition and string concatenation are two completely different operations and TypeScript needs to figure out which one to do.

And this is exactly why TypeScript assesses the data type of every value you use in your program. In order to be able to make these decisions when needed.

In our example, when it reads a + b, TypeScript checks the data type of a and b. If both are string, it performs string concatenation. If both are numbers, it performs addition.


Invalid operations:

Assessing data types is also helpful for TypeScript to make sure that what you do with the data actually makes sense.

For example, computers can perform mathematical operations on numbers, but not on strings. Of course, nobody can stop you from writing a program like this:

let a = "peanut";
let b = "butter";

console.log(a * b); // Output: NaN

The TypeScript compiler reports this as an error (The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type), but the JavaScript it still writes runs and outputs NaN.

This is the way of TypeScript telling you: "Hey, I'm a little confused. How do you multiply two strings? I've never heard of that."

Some languages like Python wouldn't even let this code run, but throw a TypeError: can't multiply sequence by non-int of type 'str'. At run time, JavaScript is more permissive, which is not necessarily a good thing, and that is why the TypeScript compiler checks types for us.

As a programmer, I'd rather be told when I made a mistake early on so I can fix it, instead of running into problems later on and not knowing what the root of those problems is.


Assignment
Follow the Coding Tutorial and let's practice with data types!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 4
    Ctrl + Enter to check