0
0
Javascriptprogramming~15 mins

Ternary operator in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Ternary Operator in JavaScript
📖 Scenario: You are creating a simple program to check if a person is old enough to vote. Voting age is 18 years or older.
🎯 Goal: Build a program that uses the ternary operator to decide if a person can vote based on their age.
📋 What You'll Learn
Create a variable called age with a specific number value.
Create a variable called votingAge and set it to 18.
Use the ternary operator with age and votingAge to create a variable canVote that holds the string 'Yes' or 'No'.
Print the value of canVote.
💡 Why This Matters
🌍 Real World
Checking age eligibility is common in many apps like voting, driving licenses, or age-restricted content.
💼 Career
Understanding the ternary operator helps write clean and concise code, a useful skill for any developer.
Progress0 / 4 steps
1
Set the person's age
Create a variable called age and set it to 20.
Javascript
Need a hint?

Use const age = 20; to create the variable.

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

Use const votingAge = 18; to set the voting age.

3
Use the ternary operator to check voting eligibility
Create a variable called canVote that uses the ternary operator to check if age is greater than or equal to votingAge. If yes, set canVote to 'Yes', otherwise 'No'.
Javascript
Need a hint?

Use the ternary operator like this: condition ? valueIfTrue : valueIfFalse.

4
Print the voting eligibility
Write a console.log statement to print the value of canVote.
Javascript
Need a hint?

Use console.log(canVote); to show the result.