Verify if a User's Subscription is Still Active with If Statements
Hey there, friend! Welcome to this tutorial on If Statements in JavaScript. You've already learned about Output Statements, Variables and Data Types, Template Literals, Arithmetic Operations, and Boolean Logic. Now, it's time to take your skills to the next level with If Statements.
The Task: Verify a User's Subscription Status
Imagine you're a developer at a popular streaming service, and you need to write a script that checks if a user's subscription is still active. You'll use If Statements to achieve this.
Get Started!
Open your index.js
file and let's get started!
Step 1: Declare Variables
Create two variables: subscriptionEndDate
and today
. Set subscriptionEndDate
to a date string in the format "YYYY-MM-DD" (e.g., "2023-03-15"), and today
to the current date.
let subscriptionEndDate = "2023-03-15"let today = "2023-03-10"
Step 2: Write the If Statement
Create an If Statement to check if the subscriptionEndDate
is greater than or equal to today
. If true, the subscription is still active.
if (subscriptionEndDate >= today) { console.log("Subscription is still active!")} else { console.log("Subscription has expired.")}
Run the Code!
Click the "Run" button to execute the code. You should see the output in the console.
How it Works:
The If Statement checks if the subscriptionEndDate
is greater than or equal to today
. If the condition is true, it logs "Subscription is still active!" to the console. If the condition is false, it logs "Subscription has expired."
Try it Out!
Change the subscriptionEndDate
to a date that's before today
. Run the code again. What happens?
What's Next?
Now that you've mastered the basic If Statement, let's move on to more complex scenarios. In the next tutorial, you'll learn about Else If Statements and how to use them to handle multiple conditions.
For now, take a moment to experiment with the code. Try changing the subscriptionEndDate
and today
variables to see how the output changes. When you're ready, click the "Next" button to proceed to the next tutorial.
Happy coding, and I'll see you in the next tutorial!
Tests