Repeating the same value over and over gets tedious — and error-prone. Variables let you name a value once and reuse it. In this lesson you'll store a name and an age in variables, then build a sentence that includes both.
What you'll learn
- Declare variables with
constandlet - Combine strings with
+or template literals - Pick
constvsletbased on whether the value should change
Instructions
Create two variables:
name, a string, set to"Ada"age, a number, set to36
Then print exactly:
Hi, I'm Ada and I'm 36 years old.
Use at least one const in your solution (any of the variables is fine — they don't change).
Key concepts
Declaring variables
const name = "Ada";
let age = 36;
constdeclares a value that won't be reassigned. Use it by default.letdeclares a value that will change (counters, accumulators, etc.).
Reassigning a const throws an error; reassigning a let is fine:
let count = 0;
count = count + 1; // OK
const pi = 3.14;
pi = 3; // TypeError: Assignment to constant variable.
Joining strings
Two ways to build a sentence out of variables:
Concatenation with +:
console.log("Hi, I'm " + name + " and I'm " + age + " years old.");
Template literals (backticks):
console.log(`Hi, I'm ${name} and I'm ${age} years old.`);
Template literals are usually easier to read — especially when you have multiple variables or want to write text that includes quote characters. The ${...} syntax drops a variable (or any expression) straight into the string.
Hints
- You need two variables and one
console.log. - Use
const name = "Ada";and pickconstorletforage. console.log(\Hi, I'm ${name} and I'm ${age} years old.\);