Apps Script Course: Conditions (continued)
When comparing two strings, remember that case sensitivity is taken into account.
Uppercase / Lowercase
To ignore case sensitivity, you can convert the tested value to lowercase (or uppercase) with the toLowerCase (or toUpperCase) method:
const text = 'Sheets-Pratique';
if (text.toLowerCase() == 'sheets-pratique') {
console.log(text); // Displays: Sheets-Pratique
}
Length
For a condition based on the length of a string, use the length property (as with arrays):
const text = 'Sheets-Pratique';
if (text.length === 15) {
console.log(text); // Displays: Sheets-Pratique
}
Contains
To check if a string contains a value, use the includes method:
const text = 'Sheets-Pratique.com';
if (text.includes('Pratique')) {
console.log('Yes'); // Displays: Yes
}
Starts With
To check if a string starts with a value, use the startsWith method:
const text = 'Sheets-Pratique.com';
if (text.startsWith('Sheets')) {
console.log('Yes'); // Displays: Yes
}
Ends With
To check if a string ends with a value, use the endsWith method:
const text = 'Sheets-Pratique.com';
if (text.endsWith('.com')) {
console.log('Yes'); // Displays: Yes
}
Trim
If the string may contain unwanted spaces at the beginning or end, use the trim method to remove them:
const text = ' Sheets-Pratique.com '.trim();
console.log(text); // Displays: Sheets-Pratique.com
Variable Scope
It's time to discuss the scope of variables and constants...
They have a scope limited to the block in which they are declared (a block is delimited by a pair of {}).
Let's take an example:
const test = true;
if (test) {
let result = 'yes';
} else {
let result = 'no';
}
console.log(result); // ReferenceError: result is not defined
Here, the variable result is declared within the block of the condition, so it is not possible to access this value outside of the block.
To solve this problem, the variable should be declared before the condition:
const test = true;
let result;
if (test) {
result = 'yes';
} else {
result = 'no';
}
console.log(result); // Displays: yes
Indentation and Code Readability
You may have noticed that the different examples are always indented, meaning the code inside a block is shifted to the right for better readability of the code (and to more easily visualize the different blocks).
In addition to indentation, also think about choosing explicit variable names, leaving spaces, and commenting on your code.
For example, try to understand this code and visualize the different blocks:
function demo() {
const t=4;
let m='Error';
if(t>0){
if(t%2==0){
if(t<10){
m='Even number less than 10';
}else if(t===10){
m='Even number equal to 10';
}else{
m='Even number greater than 10';
}
}else{
m='Odd number';
}
}
console.log(m);
}
Here is now the same code but more readable, indented, spaced, and commented:
function demo() {
const test = 4;
let message = 'Error';
// If test is greater than 0
if (test > 0) {
// If test is an even number
if (test % 2 == 0) { // The remainder of dividing even numbers by 2 = 0
// Message depending on the value of test
if (test < 10) {
message = 'Even number less than 10';
} else if (test === 10) {
message = 'Even number equal to 10';
} else {
message = 'Even number greater than 10';
}
// If test is an odd number
} else {
message = 'Odd number';
}
}
console.log(message); // Displays: Even number less than 10
}
Comments
Sometimes you may need to comment out several lines of code (for example, to test only part of the code, or not to delete certain lines right away).
To comment out multiple lines, simply surround the concerned code with /* and */:
function demo() {
const test = 4;
let message = 'Error';
/*
// If test is greater than 0
if (test > 0) {
// If test is an even number
if (test % 2 == 0) { // The remainder of dividing even numbers by 2 = 0
// Message depending on the value of test
if (test < 10) {
message = 'Even number less than 10';
} else if (test === 10) {
message = 'Even number equal to 10';
} else {
message = 'Even number greater than 10';
}
// If test is an odd number
} else {
message = 'Odd number';
}
}
*/
console.log(message); // Displays: Error
}