Start learning free

LESSON 10 OF 410 · VARIABLES

Data Types in JavaScript

Lesson

TL ; DR:

In JavaScript, 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, JavaScript 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 JavaScript 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
  • None - represents a null value, or no value at all

typeof operator

JavaScript knows what type everything is. We can ask JavaScript 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 JavaScript needs to figure out which one to do.

And this is exactly why JavaScript 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, JavaScript 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:

Asessing data types is also helpful for JavaScript 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

But when you try to run this program, it will output NaN.

This is the way of JavaScript 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'. JavaScript is more permissive, which is not necessary a good thing.

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