0
0
Javascriptprogramming~15 mins

Block scope in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Block Scope in JavaScript
πŸ“– Scenario: Imagine you are organizing a small event and you want to keep track of the number of guests in different rooms. Each room has its own count that should not affect the others.
🎯 Goal: You will learn how to use block scope with let to keep variables limited to their blocks. This helps avoid mistakes when variables with the same name are used in different parts of your code.
πŸ“‹ What You'll Learn
Create a variable with let inside a block
Create another variable with the same name in a different block
Show that the variables do not interfere with each other
Print the values of the variables to see block scope in action
πŸ’‘ Why This Matters
🌍 Real World
Block scope helps when you work on projects with many parts, like web pages with different sections or apps with multiple features. It keeps variables safe inside their areas.
πŸ’Ό Career
Understanding block scope is important for writing clean, bug-free JavaScript code, a key skill for web developers and software engineers.
Progress0 / 4 steps
1
Create a variable guestCount with value 5 outside any block
Write a line of code to create a variable called guestCount using let and set it to 5 outside any block.
Javascript
Need a hint?

Use let guestCount = 5; to create the variable.

2
Create a block and inside it create a new guestCount variable with value 10
Add a block using curly braces { }. Inside this block, create a new variable called guestCount using let and set it to 10.
Javascript
Need a hint?

Use curly braces to create a block and declare let guestCount = 10; inside it.

3
Inside the block, add a console.log to print the inner guestCount
Inside the block you created, add a line to print the value of guestCount using console.log.
Javascript
Need a hint?

Use console.log(guestCount); inside the block to print the inner variable.

4
Print the outer guestCount variable outside the block
Add a line after the block to print the value of the outer guestCount variable using console.log.
Javascript
Need a hint?

Use console.log(guestCount); after the block to print the outer variable.