VAR vs CONST vs CONSTANT in Apps Script

About this Tutorial

Level up your variable assignments in this quick video on why there is Var or Const, or a Constant.

Video Transcript

0:00 This is a quick programming video tutorial just to talk about variables and constants. I'm creating an Apps Script here that's sending emails and actually sending kind, all kinds of messages and stuff.
0:13 So, this is a product Sheetsend that I'll be releasing soon and as I'm updating it, I realize I have variables, const, and constants here and I wanted to explain to you exactly what the difference is and why we may do these things because the reason they exist is very different than why we may do this
0:32 . So, one pretty cool thing in JavaScript and also Apps Script is that we can assign Variables and variables are like this variable ui get you especially dot get ui we have variables for grabbing data from places or in JavaScript what we can do with a variable is change it.
0:56 We can say, hey, this variable is assigned this, but we want to add to it. So, if we're creating, like, say, HTML body emails, we can take a variable body then add to that variable body with a plus equal sign.
1:11 And so, variables are could change over the course of an app script. However, a const is saying, we're setting this now and we only want to set it now.
1:23 It's not going to change. There is another thing called a constant. And this you can see at the top of my app script here is all caps with an equal sign.
1:32 It does not have the word V-A-R and it does not have the word C-O-N-S-T. You see at the beginning, it's all caps, it's pink, and it equals something.
1:41 Now all of these are equal to, uh, empty fields because what I've done here is when I'm making a product that I know someone else is going to have to add an API key, a URL, an email address, but they don't necessarily, they're not going to do it inside the spreadsheet.
1:59 So they're setting it Some things, like, I always have this sort of settings page where I can do some settings and they can add, put in something, but something like a, like a auth token or a phone number, they might want to hide it a little bit, even though maybe they're the only ones using the sheet
2:18 or someone else is using it and they just don't want to make it show. It might not be a huge vulnerability, like I would not want, uh, API keys that other people can steal and use here, but if it's like an internal source, a resource, and I just want to put an API key somewhere where everyone has access
2:37 to the API key, but I just don't want them to see it necessarily in the settings tab, right? I can say, hey, once you get the sheet, copy it and put your, uh, you know, auth token here in in here.
2:50 So, but that is only being sent once for the entire app script. And now what's really cool is I can use this variable or this actually constant anywhere in any of the functions.
3:07 So I could create multiple functions that use an auth token or use a phone number or use a web app URL.
3:13 I can use those multiple times in different functions doing different things. Whereas I would normally, if I didn't have this available to me, this constant, I would have to set that each and every time and remember, okay, I have to have all of these variables or all of these constants available to me
3:30 in this particular function. Now, that, to a early new programmer, may be best. If you're just new to programming and you want to make sure each and every function works as you expect it to, then assign each variable there.
3:52 But what you can do later on, as you get more interested in these kinds of nuances and uses, is set up start changing your variables to const when you only apply it once, create constants where you use the same thing over and over again.
4:09 Let me show you one other example. In the product sheet ops that I just created recently, uh, there are columns for things.
4:16 And I realized that users of these columns may want to switch them around, meaning just move them around or add a column or.
4:25 Move columns somewhere. So what I've done in the app script is I've assigned a column number, a name. So this constant is saying task name column is 2. So if for some reason you want to change the task name column, maybe you want to add a column to the left, that would change to 3. also all of them would
4:45 change in that particular case. But this task name column appears now 8 times, in different functions, all over the sheet doing different things.
5:00 And I think that's a pretty cool use case because it allows us to create app script that does look at the structure of the sheet, but can be updated if someone needs it.
5:13 Instead of having to search for the number 2, which is 59 times. There. We can also do comma 2, and see we have no uses of that because I've switched them all out for that constant, that task name column.
5:31 So, hopefully that was an interesting video for you. Again, this is more connected. It's more over actual, like, JavaScript, so I wanted to separate this video out from other tutorial videos around Apps Script, just in case you weren't ready for it yet.
5:49 But I think this is a really cool example of how Apps Script, like JavaScript, can be very, very useful in next level, next level, next level.
6:00 So, hopefully this is helpful. Leveling you up in your knowledge of Apps Script. Thank you so much.

Courses

Spreadsheet Automation 101: Introduction to Pre-course Videos

Think Like a Programmer: Develop The Mindset of an Apps Script Coder

How To Change Date Format Automatically

Tips to Navigating Thousands of Lines of Code In Apps Script

Spreadsheet Automation 101: Functions

Spreadsheet Automation 101: Variables

Spreadsheet Automation 101: Dot Notation

Spreadsheet Automation 101: Camel Case

Spreadsheet Automation 101: Parentheses

Spreadsheet Automation 101 Lesson 1: GetValue - Introduction to SpreadsheetApp

Spreadsheet Automation 101 Lesson 1: Spreadsheet Taxonomy

Spreadsheet Automation 101 Lesson 1: A1 Notation vs Row,Column Syntax

Spreadsheet Automation 101 Lesson 1: getActiveSpreadsheet() vs getActiveSheet()

Spreadsheet Automation 101 Lesson 1: onOpen() Trigger - Custom Menu

This Seems Like Automation

Spreadsheet Automation 101 Lesson 2: Get Values - Introduction

Spreadsheet Automation 101 Lesson 2: Arrays

Spreadsheet Automation 101 Lesson 2: For Loop

Spreadsheet Automation 101 Lesson 2: Bracket Notation

Spreadsheet Automation 101 Lesson 2: Logger.log()

Spreadsheet Automation 101 Lesson 2: If ( ){ } and Checkboxes

Spreadsheet Automation 101 Lesson 2: onEdit() Trigger

Introduction to Spreadsheet Automation 101 Lesson 3

Spreadsheet Automation 101 Lesson 3: MailApp

Spreadsheet Automation 101 Lesson 3: Email Yourself For Loop

Spreadsheet Automation 101 Lesson 3: Send Email Every Week Trigger

Spreadsheet Automation 101 Lesson 3: Email Other People For Loop

Spreadsheet Automation 101 Lesson 4: Access APIs Introduction

Spreadsheet Automation 101 Lesson 4: UrlFetchApp

Spreadsheet Automation 101 Lesson 4: OmdbAPI get ApiKey, get Data in URL

Spreadsheet Automation 101 Lesson 4: OmdbAPI get data in Apps Script

Spreadsheet Automation 101 Lesson 4: JSON (beautifier) and OmdbAPI parameters

Spreadsheet Automation 101 Lesson 4: OmdbAPI Parameter Picker

Automatically Clear Content | Refresh Reuse Recycle Templates

Automate Google Sheets With Zero Experience

Automatically Uncheck A Daily Checklist

Activate A Certain Sheet When Opening a Spreadsheet

Scoping Functions in Apps Script

Breaking Through Errors In Apps Script

VAR vs CONST vs CONSTANT in Apps Script