Javascript · Strings
Create and log
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — What's a String?
We already talked about strings earlier, but let's take a closer look at what they are and how to use them in JavaScript.
A string is simply a sequence of characters (letters, numbers, symbols) that represents text. For example:
let codemonName = "Aquachu"; let greeting = `Welcome to the Codémon world!`; let element = 'Water'; console.log(codemonName); console.log(greeting); console.log(element);
Notice how strings are wrapped in quotes. This tells JavaScript "this is text, not code".
Strings vs Numbers
In JavaScript,
"42"
(with quotes) is a string, while
42
(without quotes) is a number. They look similar but behave very differently! We'll see this difference more as we progress.
Exercise 2 — Your First String
To create a string in JavaScript, you simply wrap your text in quotes. Let's create a string to store your Codémon's name!
Here's your mission: Change the
"???"
in the code below to one of the Codémon names, then run it! Reminder: The names are Aquachu, Florachu, and Pyrochu. Choose wisely!
let myCodemon = "???"; console.log("I choose you"); console.log(myCodemon);
Exercise 3 — Three Ways to Quote
JavaScript gives you three different ways to create strings. All three work similarly for basic strings:
// 1. Single quotes let name1 = 'Aquachu'; // 2. Double quotes let name2 = "Pyrochu"; // 3. Backticks let name3 = `Florachu`; console.log(name1); console.log(name2); console.log(name3);
All three create valid strings! You might wonder: why have three ways to do the same thing?
The main reason is flexibility. Sometimes you need quotes inside your string. Look at this example:
// Using single quotes outside, double quotes inside let message1 = 'Aquachu said "I\'m ready!"'; console.log(message1);
What about backticks?
Backticks have special powers we'll learn later! For now, just know they work like regular quotes for simple strings. They're called template literals and have extra features we'll explore in future lessons.
Key rule: You must start and end with the same type of quote!
'hello"
won't work.
Exercise 4 — Choosing the Right Quotes
You want to create a string that says:
It's Pyrochu's turn!
Which of these options will work correctly?
Options
• ```let message = "It's Pyrochu's turn!";```
• ```let message = 'It's Pyrochu's turn!';```
• ```let message = "It"s Pyrochu"s turn!";```
• All of them work the same way
Hint : Look carefully at where the apostrophes (') appear in the text. If you use single quotes to wrap the string, JavaScript will think the string ends at the first apostrophe inside!
Exercise 5 — Creating Strings Practice
Complete the code to create three different strings about Codémon, each using a different type of quote.
// Using single quotes let codemon1 = ____Aquachu____; // Using double quotes let codemon2 = ____Pyrochu____; // Using backticks let codemon3 = ____Florachu____; console.log(codemon1, codemon2, codemon3);
Options: ' " ` ' " `
Hint : Remember: strings need matching quotes at the start and end. Single quotes ```'```, double quotes ```"```, and backticks `````` each need to match themselves.
Exercise 6 — Match the Quote Type
Match each string scenario with the most appropriate quote type to use.
Items to match
• Simple text with no special characters
• Text containing an apostrophe: It's
• Text with double quotes inside: She said "hello"
Definitions
• Any quotes work: 'text' or "text" or text
• Double quotes: "It's great!"
• Single quotes: 'She said "hello"'
Exercise 7 — Combining Strings Together
Often, you'll want to combine multiple strings into one. This is called string concatenation, and JavaScript makes it super easy with the
+
operator!
let firstName = "Pierre"; let codemon = "Aquachu"; // Combining strings with + let message = firstName + " caught " + codemon + "!"; console.log(message);
You can concatenate as many strings as you want:
let trainer = "Pierre"; let codemon1 = "Pyrochu"; let codemon2 = "Florachu"; let codemon3 = "Aquachu"; let team = trainer + "'s team: " + codemon1 + ", " + codemon2 + ", and " + codemon3; console.log(team);
Don't forget the spaces!
A common beginner mistake is forgetting spaces when concatenating.
"Hello" + "World"
gives
"HelloWorld"
, but
"Hello " + "World"
gives
"Hello World"
. Those spaces inside quotes matter!
Exercise 8 — What's the Result?
What will this code output?
let type1 = "Fire"; let type2 = "Water"; let result = type1 + type2; console.log(result);
Options
• ```FireWater```
• ```Fire Water```
• ```Fire + Water```
• An error occurs
Hint : When you concatenate strings with ```+```, they join directly together. There are no spaces unless you add them yourself inside quotes!
Exercise 9 — Your First Concatenation
Use the
+
operator to combine these three strings into one message.
let codemon = "Aquachu"; let type = "Water"; let message = codemon ____ " is " ____ type ____ " type"; console.log(message);
Options: + + + + - * =
Hint : To join strings together, use the ```+``` operator between them, just like adding numbers!
Exercise 10 — Your First Codémon Journey
It's time to start your own Codémon journey! In this challenge, you'll create your trainer profile. Your mission: 1. Create a variable
trainerName
with your name (or any name you choose) 2. Create a variable
firstCodemon
with your starter Codémon's name (choose: Aquachu, Pyrochu, or Florachu) 3. Use string concatenation to create a message that says:
"[Name]'s first Codémon is [Codémon]!"
4. Display the message with
console.log()
Example output:
"Pierre's first Codémon is Aquachu!"
Starter code
// Create your trainer name // Create your first Codémon // Build your message using concatenation // Display the message
Back to the course