Bird
Raised Fist0
Javascriptprogramming~10 mins

Output formatting basics in Javascript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Output formatting basics
Start
↓
Prepare data
↓
Choose format method
↓
Apply formatting
↓
Print or return formatted output
↓
End
This flow shows how data is prepared, formatted, and then output in JavaScript.
Execution Sample
Javascript
const name = "Alice";
const age = 30;
console.log(`Name: ${name}, Age: ${age}`);
This code formats and prints a string with variables using template literals.
Execution Table
StepActionEvaluationResult
1Declare variable namename = "Alice"name holds "Alice"
2Declare variable ageage = 30age holds 30
3Format string with template literal`Name: ${name}, Age: ${age}`"Name: Alice, Age: 30"
4Print formatted stringconsole.log outputName: Alice, Age: 30
5End of codeNo more statementsExecution stops
💡 All statements executed, program ends
Variable Tracker
VariableStartAfter Step 1After Step 2Final
nameundefined"Alice""Alice""Alice"
ageundefinedundefined3030
Key Moments - 2 Insights
Why do we use backticks (`) instead of quotes for formatting?
Backticks allow embedding variables directly inside the string using ${}, as shown in step 3 of the execution_table.
What happens if we use single or double quotes instead of backticks?
Using single or double quotes treats the string literally, so variables won't be replaced, unlike the formatted output in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' after step 1?
A30
B"Alice"
Cundefined
Dnull
💡 Hint
Check the 'Evaluation' and 'Result' columns in row for step 1.
At which step is the formatted string created?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where template literals are evaluated in the execution_table.
If we replaced backticks with double quotes in the code, what would be the output at step 4?
AName: ${name}, Age: ${age}
BName: Alice, Age: 30
CName: name, Age: age
DError
💡 Hint
Refer to key_moments about how quotes affect variable substitution.
Concept Snapshot
Output formatting basics in JavaScript:
- Use backticks (`) for template literals.
- Embed variables with ${variable} inside strings.
- Use console.log() to print formatted output.
- Single/double quotes do not substitute variables.
- Template literals make output readable and dynamic.
Full Transcript
This lesson shows how JavaScript formats output using template literals. First, variables are declared with values. Then, a string is created using backticks, embedding variables inside ${}. This string is printed with console.log. Using backticks allows easy insertion of variable values into strings. Single or double quotes would print the variable names literally. The execution table traces each step, showing variable values and the final printed output. Key moments clarify why backticks are needed and what happens if replaced. The quiz tests understanding of variable values and formatting steps. The snapshot summarizes the main points for quick review.

Practice

(1/5)
1. What is the correct way to include a variable name inside a string using template literals in JavaScript?
easy
A. console.log(`Hello, ${name}!`);
B. console.log('Hello, ' + name + '!');
C. console.log("Hello, ${name}!");
D. console.log('Hello, ${name}!');

Solution

  1. Step 1: Understand template literals syntax

    Template literals use backticks (`) and ${} to insert variables inside strings.
  2. Step 2: Identify correct usage

    console.log(`Hello, ${name}!`); uses backticks and ${name} correctly to embed the variable.
  3. Final Answer:

    console.log(`Hello, ${name}!`); -> Option A
  4. Quick Check:

    Backticks + ${} = correct template literal [OK]
Hint: Use backticks and ${variable} for easy variable insertion [OK]
Common Mistakes:
  • Using quotes instead of backticks for template literals
  • Forgetting ${} around variable
  • Using single or double quotes with ${} which won't interpolate
2. Which of the following is the correct syntax to print the number 5 and the word 'apples' in one line using template literals?
easy
A. console.log(`5 apples`);
B. console.log(`5 + apples`);
C. console.log('5 apples');
D. console.log(`5 ${apples}`);

Solution

  1. Step 1: Check the use of template literals

    Backticks allow strings with variables inside ${}, but here no variable is needed.
  2. Step 2: Identify correct string

    console.log(`5 apples`); correctly uses backticks with plain text '5 apples'.
  3. Final Answer:

    console.log(`5 apples`); -> Option A
  4. Quick Check:

    Backticks with plain text = valid string [OK]
Hint: Use backticks for strings even without variables [OK]
Common Mistakes:
  • Using quotes instead of backticks when template literals expected
  • Trying to insert undefined variable apples
  • Adding plus sign inside template literal as text
3. What will be the output of the following code?
const count = 3;
console.log(`You have ${count} new messages.`);
medium
A. You have ${count} new messages.
B. You have count new messages.
C. You have 3 new messages.
D. SyntaxError

Solution

  1. Step 1: Understand template literal variable insertion

    The variable count inside ${} is replaced by its value 3.
  2. Step 2: Evaluate the console.log output

    The string becomes 'You have 3 new messages.' and prints exactly that.
  3. Final Answer:

    You have 3 new messages. -> Option C
  4. Quick Check:

    Variable inside ${} = replaced by value [OK]
Hint: Variables inside ${} show their value in template strings [OK]
Common Mistakes:
  • Thinking ${count} prints literally
  • Confusing quotes and backticks
  • Expecting variable name instead of value
4. Find the error in this code snippet:
let user = 'Anna';
console.log('Welcome, ${user}!');
medium
A. Missing semicolon after let declaration
B. Single quotes do not allow variable interpolation
C. Variable user is not declared
D. Backticks are missing around the string

Solution

  1. Step 1: Check string quotes for interpolation

    Single quotes ('') do not process ${} as variable placeholders.
  2. Step 2: Identify correct string type for template literals

    Backticks (`) are needed to enable variable interpolation.
  3. Final Answer:

    Single quotes do not allow variable interpolation -> Option B
  4. Quick Check:

    Use backticks for ${} interpolation [OK]
Hint: Use backticks, not single quotes, for ${} variables [OK]
Common Mistakes:
  • Thinking single quotes support ${} interpolation
  • Ignoring missing backticks
  • Assuming variable is undeclared
5. You want to print a message showing a user's name and age using template literals. The variables are name = 'Sam' and age = 25. Which code correctly formats the output as:
Hello, Sam! You are 25 years old.
hard
A. console.log(`Hello, ${name}! You are age years old.`);
B. console.log('Hello, ' + name + '! You are ' + age + ' years old.');
C. console.log(`Hello, name! You are age years old.`);
D. console.log(`Hello, ${name}! You are ${age} years old.`);

Solution

  1. Step 1: Use template literals with variables

    Backticks and ${} allow embedding variables name and age inside the string.
  2. Step 2: Check each option for correct variable usage

    console.log(`Hello, ${name}! You are ${age} years old.`); correctly uses ${name} and ${age} to insert values.
  3. Final Answer:

    console.log(`Hello, ${name}! You are ${age} years old.`); -> Option D
  4. Quick Check:

    Backticks + ${var} for each variable = correct [OK]
Hint: Use ${variable} inside backticks for multiple variables [OK]
Common Mistakes:
  • Using variable names without ${} inside template literals
  • Mixing quotes and backticks incorrectly
  • Concatenating strings instead of using template literals
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard