Skip to main content

Command Palette

Search for a command to run...

Variable scope in javascript

Published
โ€ข3 min readโ€ขView as Markdown
Variable scope in javascript

Hey there, little coder! ๐Ÿ‘‹ Imagine you have a toy box. ๐Ÿงธ Everything you put in that box is yours to play with, right? Nobody else can just come into your room and grab your toys unless you're playing with them together.

That's kind of like variable scope in JavaScript. A variable is like a little box where you can put a piece of information, like a number or a word. The scope is the "room" where that box lives and who can see and use it.

The "Global" Toy Room

Think of your entire house as the global scope. If you leave a toy in the living room, anyone in the house (your parents, siblings, even the cat! ๐Ÿฑ) can see it and play with it.

In JavaScript, a global variable is like that toy. You create it outside of any specific function or block of code. This means any part of your code can see it and change it.

Eg:-
let myToy = "Dino"; // This is a global variable

function playWithToy() { console.log("I'm playing with my " + myToy); }

playWithToy(); // It works!

This is super easy, but it can get messy. If lots of people are playing with the same toy, they might accidentally break it or change it in a way you didn't want. That's why we have "private" rooms.

The "Local" Bedroom ๐Ÿ›๏ธ

Now, imagine your bedroom. If you put a toy in your bedroom, only you and anyone you invite in can play with it. Your parents in the kitchen can't just reach in and grab it.

In JavaScript, this is called local scope. When you create a variable inside a function, it's a local variable. It's private! Only the code inside that function can see or use it.

Eg:-

function makeASandcastle() { let sandCastle = "tall and strong"; // This is a local variable console.log("My sandcastle is " + sandCastle); }

makeASandcastle(); // This works!

// console.log(sandCastle); // This will cause an error! // The 'sandCastle' variable is hiding inside the function and can't be seen here!

See? The sandCastle variable is a secret just for the makeASandcastle function. Once the function is done, the variable disappears. This keeps things organized and prevents your code from accidentally messing with each other's variables.

A "Block" is Like a Play-Pen

Sometimes, you need an even smaller, more private space, like a play-pen! ๐Ÿ‘ถ This is called block scope. You can create a block of code with curly braces {}.

If you use let or const to create a variable inside these braces, it can only be seen inside that block.

Eg:-

if (true) { let favoriteFood = "Pizza"; // This is a block-scoped variable console.log("I love " + favoriteFood); }

// console.log(favoriteFood); // This will give an error! // 'favoriteFood' is trapped inside the curly braces.

So, remember:

  • Global variables are toys in the living room. Everyone can see them.

  • Local variables are toys in your bedroom. Only you can see them.

  • Block variables are toys in a play-pen. They are super private and temporary!

Understanding this helps you write cleaner, safer code that doesn't get all tangled up like a pile of LEGOs! ๐Ÿงฑ Happy coding!