Variables and constants in Apps Script

Variables are used to store values.

To assign a value to a variable, you need to first declare the variable and then assign a value to it.

Declaring a variable

You can declare a variable by using the var keyword followed by the name of the variable.

var firstName;

Since you haven't yet assigned a value to the variable firstName, its value will be undefined.

Assigning a value to a variable

A value can be assigned to a variable by using the equal to (=) operator.

var firstName = "Jack";

You can also declare a variable first and assign a value to it later.

var firstName;
firstName = "Jack";

The var keyword should only be used when you declare a variable. This only happens once. Once you assign a value to a variable, you can use this value by using the variable's name.

var firstName;
firstName = "Jack";
Logger.log(firstName); //Jack

In the above example, the statement Logger.log(firstName); will output the string "Jack" because that is the value stored in the variable firstName.

Always declare a variable before assigning a value to it

Although Apps Script will allow you to assign a value to a variable that has not yet been declared, please do not do this. This will create something called a global variable and could cause inadvertent bugs in your program.

So, do not assign a value to a variable that has not been declared. Either declare a variable and assign a value to it at the same time OR declare a variable first and then assign a value to it.

//Do not do this
firstName = "Jack"; //this will create a global variable

//This is OK - declare a variable and assign a value to it at the same time.
var firstName = "Jack";

//This is also OK - declare a variable first and then assign a value to it.
var firstName; //declaration first
firstName = "Jack"; //followed by assignment

Assigning a new value to a variable

You can change the value that is assigned to a variable. Just assign a new value to it using the equal to (=) operator.

var firstName;
Logger.log(firstName); //undefined
firstName = "Jack";
Logger.log(firstName); //Jack
firstName = "Ryan";
Logger.log(firstName); //Ryan
firstName = "Bush";
Logger.log(firstName); //Bush

Using operators with variables

You can also use operators to perform operations on variables. These operators will operate on the values contained in the variables.

var price = 10.0;
var quantity = 20;
var totalAmount = price * quantity;
Logger.log(totalAmount); //200.0

The type of a variable

A variable's type will be the same as the type of the value it contains. For example, if the variable contains a string value then its type is also string. If you haven't assigned any value to a variable, its value will be undefined.

var firstName;
Logger.log(typeof firstName); //undefined
firstName = "Jack";
Logger.log(typeof firstName); //string

Variable names

Apps Script has certain rules that govern variable names.

  • Variable names can only contain letters, numbers, underscores (_) or dollar signs ($).
  • Variable names must start with a letter, a dollar sign ($) or an underscore (_). They cannot start with a number.
  • Variable names cannot contain spaces.
  • You cannot use certain words as variable names. These words are called reserved words. For example, you cannot use var as a variable name. The full list of reserved words can be found here.

Best practices for naming variables

There are also some best practices that you should consider following when naming variables.

  • Pick a name that is meaningful. Someone else reading your code should be able to understand the purpose of a variable from its name. For example, the name aaa does not tell us anything about the type of values it might contain. The name firstName tells us a lot about the purpose of the variable and the values it might contain.
  • If the name contains multiple words, either use underscores or camel casing to make it easier to read the name. It is very hard to read the nametimesheetdueby. It is a lot easier to read time_sheet_due_by or timeSheetDueBy.

Variable scopes

We'll discuss variable scopes in detail later. For now, it is enough if you know the following:

  • Variables can either be local in scope or global in scope.
  • Variables declared within a function are local in scope. This means that their values can only be accessed within the function where they are declared.
  • Variables that are declared outside of a function are global in scope. This means that their values can be accessed anywhere in your program (even within functions).

Constants

Constants are just like variables except you cannot change its value after assigning one to it. To declare a constant, use the const keyword instead of var.

const TAX_RATE = 0.05; //5% tax rate
Logger.log(TAX_RATE); //0.05
TAX_RATE = 0.10; //try changing it to 10%
Logger.log(TAX_RATE); //0.05 (the value did not change to 10% - it remained constant)

It is a best practice to use CAPS while naming constants so it is easy for someone reading your code to differentiate variables from constants. Also, use underscores to separate multiple words in the name.

var price = 10.0;
var quantity = 20;
const TAX_RATE = 0.05; //5% tax rate
var totalAmount = price * quantity * (1 + TAX_RATE); //order amount + tax

Have feedback for me?

I'd appreciate any feedback you can give me regarding this post.

Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!