Values, types and operators in Apps Script
Introduction
In Apps Script, values are the data used by your code. The code you write will usually accept one or more values as input, process them and then output the resulting values. Without values there wouldn't be much for your code to do. This lesson introduces the following concepts: Values, Types and Operators.
This lesson is part of the Introduction to coding using Apps Script course.
Prerequisites
You've completed the previous lessons in this course OR are familiar with the following concepts:
What is Apps Script?
How to create and run scripts?
What is logging and how to log values from your script?
Values
There are six types of values in Apps Script. A value's type tells you the type of data it is. The six types are:
Number: Any numerical value (e.g., 3.15, 27, -50, 1e4)
String: A piece of text within quotation marks (e.g., "apple", "school", "Hi there!", "1234")
Boolean: A value that is true or false. (Note: there are no quotes surrounding boolean values, i.e., they are not strings)
Object: A value that contains other values.
Function: A value that is reusable code.
Undefined: A value that is empty so its type is unknown.
You will learn more about objects and functions later on in this course. For now just know that values can be simple (like the number 3) or complex (like an object that contains other values).
Operators
Operators are used to perform operations on one or more values. For example, the addition operator (+) is used to add multiple values together.
There are three main types of operators: Numeric, Comparison and Logical.
Numeric operators
Numeric operators are used to perform mathematical operations on numeric values. There main ones are:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Remainder: %
Examples:
Logger.log(1 + 2); // The value 3 is logged.
Logger.log(3 * 4); // The value 12 is logged.
Logger.log(1 + 2 + 7); // The value 10 is logged.
Logger.log(25 % 3); // The value 1 is logged.
1 + 2
3 * 4
1 + 2 + 7
25 % 3
Operator precedence
What is the value that gets logged when you execute the following code?
Logger.log(2 + 2 * 4);
It depends on which operation occurs first, doesn't it? If you do the addition first you get 4 * 4
which is 16. If you do the multiplication first, you get 2 + 8
which is 10. Is the output 16 or is it 10?
There is only one correct answer and that is 10. This is because Apps Script has rules for what to do in situations like this. These rules are called operator precedence rules.
You can force Apps Script perform a certain operation first by using parentheses (round brackets). The expression within the parentheses will be evaluated first.
2 + (2 * 4)
: The multiplication will happen first followed by the addition.(2 + 2) * 4
: The addition will happen first followed by the multiplication.
Insead of learning all of the operator precedence rules in Apps Script, just use parentheses in situations like this to ensure Apps Script does whatever it is that you want it to do.
Comparison operators
Comparison operators are used to compare two values. For example, the > (greater than) operator is used to check if a value is greater than another value.
When you compare two values the result is either true or false, i.e., a boolean value. For example, 5 > 1 results in true but 1 > 20 will result in false.
There are six comparison operators. They are:
Greater than (>): a > b results in true if the value a is greater than value b and false if a is less than or equal to b.
Greater than or equal to (>=): a >= b results in true if the value a is greater than or equal to value b and false if a is less than b.
Less than (<): a < b results in true if the value a is lesser than value b and false if a is greater than or equal to b.
Less than or equal to (<=): a <= b results in true if the value a is lesser than or equal to value b and false if a is greater than b.
Equality (==): a == b results in true if the value a is equal to value b.
Strict equality (===): a === b results in true if the value a is equal to value b and their types are also the same.
Inequality (!=): a != b results in true if value a is not equal to value b. Please note that the inequality operator will attempt to convert and compare values of different types.
Strict inequality (!==): a !== b results in true if the value a is not equal to value b OR if their types are different.
What is the difference between == and === OR between != and !==?
To understand the difference, you need to understand a concept called type conversion.
When you compare the number 1 with the text "1" using the equals (==) operator, the result is true
. How can a number be equal to text? This is due to automatic type conversion.
When you compare two values of different types, Apps Script converts them to the same type before comparing them. To prevent Apps Script from converting types when comparing values, use the strict equality operator (===) or the strict inequality operator (!==).
Logical operators
Logical operators operate on boolean values with the result also being a boolean value. There are three logical operators and they are:
AND (&&): a && b results in true only if both a and b are both true. It results in false if either one or both of them are false.
OR (||): a || b results in true if either one or both values are true. It results in false only if both a and b are both false.
NOT (!): false becomes true and vice versa. For example, !a results in true if a is false.
It is common for Comparison and Logical operators to be used together
Imagine that a school administrator wants to figure out which students are eligible for a scholarship. The school has the following policy for awarding these scholarships. In order to be eligible, a student must meet both of the following conditions:
Their parents' income must be less than or equal to $70K.
Their GPA must be greater than 3.5.
The above eligibility criteria can be implemented in the following way:
(income <= 70000) && (gpa > 3.5)
The typeof operator
The typeof
operator is used to find out the type of a given value. The code examples below demonstrate how this operator works.
//Numbers
Logger.log(typeof 12.3); //number
Logger.log(typeof 1e10); //number
Logger.log(typeof -71.23); //number
Logger.log(typeof 121); //number
//Boolean
Logger.log(typeof true); //boolean
Logger.log(typeof false); //boolean
//Strings
Logger.log(typeof "Hello world!"); //string
Logger.log(typeof "1234"); //string
Logger.log(typeof "%*&^*#^"); //string
Conclusion
In this lesson, you learned about values, types and operators in Apps Script.
Values are the data used by your program.
Every value in Apps Script has a type. There are six types and they are: number, string, boolean, function, object and undefined.
Operators are used to perform operations on values. There are three main types of operators: Numeric, Comparison and Logical operators.
Master Google Sheets Automation
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!