Welcome to the third edition of “Spot the Bug”
We have a person object with a name property and a greet method. We then assign the greet method to a variable called greetFunction and call it.
Run the code yourself, do you see the person’s name being logged to the console? If not, why?
Take a moment to think, and when you’re ready, hit reply with your answer!
In the second edition of Spot the Bug ↗️, we presented the following code snippet, were you able to solve it?
Let’s understand the bug and its resolution in more detail.
In this code, what we wanted was to find the largest number from the array however the bug is that it logs
undefined for largestNumber when calling
This occurs because the largest variable is declared within the if block, but its scope extends beyond the block due to variable hoisting, leading to an
To fix this bug, you can declare the largest variable outside the loop and initialize it to a suitable initial value before the loop, like this:
See you in the next edition of “Spot the Bug”!
- Icons from Freepik ↗️