GAS – Values, Types and Operators in Apps Script


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.

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.

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: %

Example

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

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 both values are of the same type but value a is not equal to value b.
  • Strict inequality (!==): a !== b results in true if the value a is not equal to value b OR if their types are different.

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.

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.
Nandemo Webtools

Leave a Reply