IF Statements
A foundational building block of all software engineering
Experience Level: Beginner
JavaScript Concepts
Date Object
Function
Conditional
In my opinion, the best part about computer programming is handing over basic decision-making and redundant computation to a computer. There are so many things in life to juggle, but once you learn how to build applications effectively, you can outsource some of that decision-making and work to a computer that will do it faster and more accurately than you can (as long as you write the program correctly!)
In this tutorial, we will build a function where you can pass in a date, and the function will tell you if you have passed the due date. Though simple, this is crucial to any good to-do list or reminders application. For example, I will use this functionality to build my car maintenance tracker. IF today is past the recommended service date, I will automagically add a card to my Trello!
Let's open up your Apps Script environment and get started!
Let’s start by declaring the function and ensure we can get today’s date.
function checkDueDate(){
let today = new Date();
console.log(today)
}
When you click “Run” I would expect to see a date in a format similar to the one below.
Tue Nov 08 2022 11:53:26 GMT-0500 (Eastern Standard Time)
Now, we need to be able to define the parameters (inputs) for our function that we will use to create the date we want to check.
function checkDueDate(year, month, day){
let today = new Date();
let dateToCheck = new Date(year, month, day)
console.log(dateToCheck)
}
To see what this inputs, we will have to write a separate function, I am going to call it main, where we will call the checkDueDate function.
function checkDueDate(year, month, day){
let today = new Date();
let dateToCheck = new Date(year, month, day)
console.log(dateToCheck)
}
function main(){
checkDueDate(2022, 11, 7)
}
In the Apps Script toolbar make sure that you see main in the toolbar. This tells Apps Script which function to run.
So, when I ran this function I was expecting to see a November date, but instead I got a December one. The reason that is the case is the built-in JavaScript Date object starts at 0. January is the 0th month, not the first. This is something that you will come across a lot as you code. Computers start at 0, not 1.
Let’s add our IF statement to our function to tell our computer to start computing and making decisions.
function checkDueDate(year, month, day){
let today = new Date();
let dateToCheck = new Date(year, month, day)
if(today > dateToCheck){
console.log('Past Due')
} else{
console.log('You got ⏱️')
}
}
Within the parentheses after the IF statement we put the math problem we want the computer to check. Is today greater than the dateToCheck. IF yes (the computer processes that as TRUE) tell us we are past due. ELSE tell us that we got time.
function main(){
checkDueDate(2022, 11, 7)
}
You got ⏱️
Phew, another deadline not missed!
This is a foundational concept that most applications are built on, if this is true, do x, if not do y. It doesn’t get too much more complex than that. The bigger the application gets there are more layers of decisions and creating integrations to get data from disparate sources, but the core of decision making is still there.
Support my work by
Other Tutorials You Might be Interested In…