0
0
Javascriptprogramming~15 mins

If statement in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
If Statement Basics in JavaScript
📖 Scenario: You are creating a simple program to check if a person is old enough to vote. In many countries, the voting age is 18 years.
🎯 Goal: Build a program that uses an if statement to check if a person's age is 18 or older and print a message accordingly.
📋 What You'll Learn
Create a variable called age with a specific number value.
Create a variable called votingAge set to 18.
Use an if statement to check if age is greater than or equal to votingAge.
Print 'You are old enough to vote.' if the condition is true.
Print 'You are not old enough to vote.' if the condition is false.
💡 Why This Matters
🌍 Real World
Checking conditions like age limits is common in websites and apps, for example, to allow access to age-restricted content.
💼 Career
Understanding if statements is a basic skill for any programming job, as it helps make decisions in code.
Progress0 / 4 steps
1
Create the age variable
Create a variable called age and set it to the number 20.
Javascript
Need a hint?

Use let age = 20; to create the variable.

2
Set the voting age
Create a variable called votingAge and set it to the number 18.
Javascript
Need a hint?

Use let votingAge = 18; to create the variable.

3
Write the if statement
Use an if statement to check if age is greater than or equal to votingAge. Inside the if block, write console.log('You are old enough to vote.');. Use an else block to write console.log('You are not old enough to vote.');.
Javascript
Need a hint?

Use if (age >= votingAge) { ... } else { ... } structure.

4
Display the result
Run the program and observe the output. It should print You are old enough to vote..
Javascript
Need a hint?

Look at the console output to see the message.