Skip to the content

Data types

JavaScript · Unit 1: First steps

Every value has a type: number for numbers, string for text and boolean for true or false.

typeof tells you what type something is.

const age = 17;
const name = "Emma";
console.log(typeof age);
console.log(typeof name);

Prints

number
string

A number inside quotes is text: "42" is a string, not a number.

And when one of the two sides is text, + doesn't add: it joins.

console.log(typeof "42");
console.log("Hello" + "world");
console.log("3" + 4);

Prints

string
Helloworld
34

Number() turns a text into a number and String() does the opposite.

That way you decide whether + adds or joins.

console.log(Number("5") + 5);
console.log(String(5) + 5);

Prints

10
55

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

    console.log(typeof true);
    console.log(typeof "true");
  2. 2. Complete the code

    Fill in to find out what type the variable stores.

    const price = 9.99;
    console.log(typeof ___);
  3. 3. Predict the output

    What does this code print?

    console.log(typeof 7);
    console.log(typeof 7.5);
  4. 4. Multiple choice

    Which of these values is text (string)?

  5. 5. Predict the output

    What does this code print?

    const age = 17;
    console.log("Age: " + age);
    console.log(age + 1);
  6. 6. Multiple choice

    What does this code do?

    const points = "10";
    console.log(points + 5);
  7. 7. Complete the code

    Fill in so it prints 18.

    const age = ___("17");
    console.log(age + 1);
  8. 8. Put the lines in order

    Order the lines to add 5 to a price that arrived as text.

  9. 9. Find the bug

    This program should print the total of 100 plus 50 for shipping. Which line has the error?

    const price = "100";
    const fee = 50;
    const total = price + fee;
    console.log("Total:", total);
  10. 10. Predict the output

    What does this code print?

    const a = "7";
    const b = 3;
    console.log(a + b);
    console.log(Number(a) + b);
Do this lesson

It opens in your browser. It is free and you do not need an account to start.

See all lessons →