Code Snippet Prediction Exercises
Improve Your JavaScript Thinking Skills
These exercises are designed to strengthen your understanding of JavaScript fundamentals
by helping you predict how code executes before running it.
Many examples include unusual or less common JavaScript behaviors that professional
developers still benefit from understanding deeply.
How to Use This Workbook
Before running any code:
1. Read the snippet carefully.
2. Predict the output.
3. Explain why the output happens.
4. Run the code in your browser console or Node.js.
5. Compare the actual result with your prediction.
6. Modify the code and test your understanding further.
The goal is not memorization — it is learning how JavaScript actually works behind the
scenes.
Priority 1 — Essential Concepts
These are the most important exercises and should be fully understood.
Exercise 1 — Assignment Inside an if Statement
Predict the Output
let myNumber = 0;
, if (myNumber = 10) {
console.log('Hacked!')
}
console.log(myNumber)
Questions
● What will be printed to the console?
● Why does the condition evaluate as truthy?
● What is the difference between:
○ =
○ ==
○ ===
Exercise 2 — Unlimited Function Arguments
Goal
Implement a function called combineNames() that works for all examples below.
Rules
● The final name should be uppercase.
● If only one name is provided, it should remain unchanged.
Examples
combineNames('David')
// Returns: 'David'
combineNames('John', 'Doe')
// Returns: 'John DOE'
combineNames('Jean', 'Marie', 'Vianney')
// Returns: 'Jean Marie VIANNEY'
combineNames('Neymar', 'Silva', 'Santos', 'Junior')
// Returns: 'Neymar Silva Santos JUNIOR'
Questions
● How can a function receive unlimited arguments?