
Scope in JavaScript
The Day I Realized My Variables Were Living in the Wrong Rooms The first time I saw this error: ReferenceError: something is not defined I thought JavaScript was broken. It wasn’t. I was. Not in a dramatic way. Just in a simple, beginner way. I didn’t understand scope. And honestly? Nobody explains scope properly. They throw definitions like: “Scope determines the accessibility of variables.” Which sounds correct… and completely forgettable. So let me explain it the way it finally made sense to me. Let’s imagine a house. The House Where Variables Live In this house, variables are like toys. Some toys are kept outside in the garden. Some are kept inside a bedroom. Some are hidden inside a small cardboard box. That’s it. That’s JavaScript scope. 1. Global Scope — The Garden Imagine a big garden outside the house. If you leave a toy there, everyone can see it. Anyone inside the house can walk out and use it. In JavaScript, when you declare a variable outside any function or block, it live
Continue reading on Dev.to Webdev
Opens in a new tab

