Primitive Types

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — What are Primitive Types?

Now that you know how to create variables, it's time to discover the different types of data you can store in them!

In JavaScript, the most basic types of data are called primitive types. They are the simplest forms of information you can work with, but they're also the foundation of everything else.

There are three main primitive types you'll use all the time: - String → Text and words (like your Codémon's name) - Number → Numbers for math and counting (like your Codémon's level) - Boolean → True or false values (like whether your Codémon is ready for battle)

// String let codemonName = "Aquachu"; // Number let codemonLevel = 15; // Boolean - true or false let isReadyForBattle = true; console.log(codemonName, codemonLevel, isReadyForBattle);

Notice how each type looks different? Strings need quotes, numbers don't, and booleans use the special words true or false. Let's explore each type in detail!

Exercise 2 — Strings: Working with Text

A string is any text you want to store in your code. It could be a single letter, a word, a sentence, or even a whole paragraph!

To create a string in JavaScript, you wrap your text in quotes. You can use: - Single quotes: 'Hello' - Double quotes: "Hello" - Backticks:`` `Hello` ``

let name1 = 'Pyrochu'; let name2 = "Florachu"; let name3 = `Aquachu`; console.log(name1); console.log(name2); console.log(name3); // You can also have empty strings let emptyString = ""; console.log(emptyString);

Why Three Types of Quotes?

Most of the time, single quotes and double quotes work exactly the same. Use whichever you prefer! Backticks are special - they allow you to insert variables directly into your strings, which you'll learn about later.

Important: Whatever quote type you start with, you must end with the same type! 'Hello" won't work.

Exercise 3 — Creating Strings

Create a variable called attack that stores the string "Water Splash".

Hint : Remember: strings need to be wrapped in quotes (single, double, or backticks). Use let to declare a variable and = to assign a value.

Exercise 4 — Which Strings are Valid?

Which of the following is a valid string in JavaScript?

Options
• 'Aquachu'
• "Aquachu'
• Aquachu
• 'Aquachu"

Hint : Remember: strings must start and end with the same type of quote, and the text must be wrapped in quotes.

Exercise 5 — Numbers: Working with Math

The number type is used for any numeric value - whether it's a whole number or a decimal!

Unlike strings, numbers are written without quotes. If you put quotes around a number, JavaScript treats it as text, not a number!

You can use whole numbers (integers) for things like levels, health points, or counts:

let codemonLevel = 25; let hp = 100; console.log(codemonLevel); console.log(hp);

You can also use decimal numbers (floats) for more precise values:

let attackPower = 15.5; let criticalChance = 0.25; console.log(attackPower); console.log(criticalChance);

Number vs String: Be Careful!

Remember: 5 is a number, but "5" is a string! They might look similar, but JavaScript treats them very differently. Numbers can be used in math operations, strings cannot.

JavaScript has some special number values: - Infinity → Represents infinity - -Infinity → Represents negative infinity - NaN → Stands for "Not a Number" (when math operations fail)

Exercise 6 — Number or String?

Look at the following code:

let value1 = 42; let value2 = "42"; let value3 = 3.14; let value4 = "3.14";

How many of these variables contain a number type?

Options
• 1
• 2
• 3
• 4

Hint : Remember: if a value is wrapped in quotes, it's a string, not a number. Look carefully at which values have quotes!

Exercise 7 — Creating Numbers

Your Pyrochu starts at level 5 with 50 HP. Create two variables to store these values.

Hint : Remember: numbers don't need quotes! Use = to assign values to variables.

Exercise 8 — Booleans: True or False

The boolean type is the simplest of all - it can only have two values: true or false.

Booleans are perfect for representing yes/no questions, on/off states, or any situation where there are only two possibilities.

let isWaterType = true; let hasEvolved = false; let isRare = true; console.log(isWaterType); console.log(hasEvolved); console.log(isRare);

True vs "True"

Just like with numbers, true and "true" are completely different! true (no quotes) is a boolean. "true" (with quotes) is a string containing the word "true". Always write booleans without quotes!

Booleans are incredibly useful when you want to make decisions in your code. You'll use them a lot when you learn about conditional statements!

Exercise 9 — Working with Booleans

Create a boolean variable called isReadyForBattle and set it to true.

Hint : Remember: boolean values true and false are written without quotes!

Exercise 10 — Match the Type

Match each value with its correct primitive type:

Items to match
• "Aquachu"
• 42
• false
• "100"
• 3.14
• true
Definitions
• string
• number
• boolean
• string
• number
• boolean

Exercise 11 — Spot the Error

Which of the following variable declarations has a value that can cause a comprehension error later?

Options
• let name = "Pyrochu";
• let level = 25;
• let isActive = "true";
• let hp = 100;

Hint : Look carefully at the types! Remember what boolean values should look like.

Exercise 12 — Create Your Codémon Profile

Time to practice! Create a simple Codémon profile using the three primitive types. Create these 3 variables with the values you want: 1. name - A string with your Codémon's name 2. level - A number for its level 3. isWaterType - A boolean (true or false) Then display them with console.log()!

Starter code
// Create your Codémon profile here!
Back to the course