Basic of Java script
JavaScript works as the fundamental language of Google Apps script. JavaScript contains both the automation and enhanced functionality for Google Workspace In this blog we will explore all the useful JavaScript concepts that are mandatory for writing an effective Apps script code.
What is the Role of JavaScript in Apps Script
Although Apps Script employs JavaScript, it has some distinct differences from the browser-based version you might be familiar with. It operates on Google's servers instead of in your browser and is specifically designed for integration with Google Workspace.
When you write Apps script code, you are writing JavaScript code, that can seamlessly interact with Google services. It demonstrates that you can manage spreadsheets, create documents, send or receive email, and manage calendars by using just a simple line of codes.
For example, By using this easy Java script function in Apps script you can easily create a new Google doc file.
function createNewDoc() {
DocumentApp.create('My New Document');
}
Variables, Data Types, and Operators
Variables are the building blocks for storing data in JavaScript. They are declared using keywords such as var, let, or const. Here’s an example:
let username = "John"; // A string variable
const age = 25; // A constant integer
Data Types include:
- String: Textual data (e.g., "Hello World")
- Number: Numeric values (example, 12, 22.14)
- Boolean: Logical values (true or false)
- Array: A collection of items (e.g., [1, 2, 3])
- Object: A collection of key-value pairs (e.g., {name: "smith", age: 35})
Operators; Allow you to perform operations on variables and values:
Arithmetic Operators: +, -, *, /
- let sum = 5 + 3; // Addition
- let difference = 10 - 4; // Subtraction
- let product = 6 * 2; // Multiplication
- let quotient = 15 / 3; // Division
Comparison Operators: ==, !=, >, <
- Equal to (==): Checks if two values are equal.
- Not equal to (!=): Checks if two values are not equal.
- Strict equal to (===): Checks if two values are equal and of the same type.
- Greater than (>): Checks if the left value is greater than the right value.
- Less than (<): Checks if the left value is less than the right value.
Logical Operators: &&, ||
- let andResult = true && false; // Logical AND
- let orResult = true || false; // Logical OR
Control Structures: Loops and Conditional Statements
Control structures help manage the flow of execution in your script. The most common ones include:
Conditional Statements
Conditional statements in JavaScript enable your program to make decisions based on particular conditions. This allows your code to execute different sections of code depending on whether certain conditions evaluate to true or false. Let’s delve into how to use if, else, and else if statements with examples.
1. Using if Statement
The if statement is used to define a block of code it will run only if the given statement is correct.
Syntax:
if (condition) {
// Block of code to be executed if the condition is true
}
Example:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
}
Explanation:
- In this example, we check if age is greater than or equal to 18.
- Since the condition is true, it prints "You are an adult."
2. Using else Statement
The else statement enables you to define a block of code that will run if the condition that we have mentioned in if statement is false.
Syntax:
if (condition) {
// Block of code to be executed if the condition is true
} else {
// Block of code to be executed if the condition is false
}
Example:
let age = 16;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are not an adult yet.");
}
Explanation:
- Here, we check if age is greater than or equal to 18.
- Since the condition is false, it prints "You are not an adult yet."
3. Implementing else if Statement
The else if statement enables you to check another condition if the first is false. You can use multiple else-if statements for different conditions.
Syntax:
if (condition1) {
// Block of code to be executed if condition1 is true
} else if (condition2) {
// Block of code to be executed if condition1 is false and condition2 is true
} else {
// Block of code to be executed if both conditions are false
}
Example:
let score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: D or below");
}
Explanation
In this example, we verify the score's value.
- When the first condition is checked If the score is 90 or higher It prints "Grade: A" if it is true.
- If the first condition is false, it determines if the score is 80 or higher for "Grade: B."
- If that’s also false, it looks for scores of 70 or higher for "Grade: C."
- If none of these criteria are satisfied. It will print "Grade: D or below"