Skip to content

Commit 19c93a0

Browse files
Fix 2.js: declare cityOfBirth before usage, add explanation
1 parent e4c6e45 commit 19c93a0

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

  • Sprint-2/2-mandatory-errors

‎Sprint-2/2-mandatory-errors/2.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Currently trying to print the string "I was born in Bolton" but it isn't working...
22
// what's the error ?
33

4-
console.log(`I was born in ${cityOfBirth}`);
54
const cityOfBirth = "Bolton";
5+
console.log(`I was born in ${cityOfBirth}`);
6+
7+
// Error:** `ReferenceError: Cannot access 'cityOfBirth' before initialization (at 2.js:4:30)`
8+
9+
// Cause:** Line 4 is trying to use `cityOfBirth` before the variable has been created. The variable is only declared on line 5, and JavaScript processes the code from top to bottom.
10+
11+
// Fix:** Declare and assign `cityOfBirth` before using it in the code.

0 commit comments

Comments
 (0)