Understanding Variable Naming in Apps Script
This post is a corrected post after AI explains the code included in the book “Google Apps Script 101: Building Work Automation For Free”. Added a strikethrough when editing what AI has written, and added color when edited by author
Table of Contents
Summary
Variable naming is an important aspect of coding in any programming language, including Apps Script. This blog post will explain the rules and conventions for naming variables in Apps Script, providing examples of both valid and invalid variable names.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function myFunction() { // Available variable name examples const variable1 // starts with an alphabet let _variable2 // starts with an underscore var $variable3 // starts with a dollar sign // Invalid variable name examples const 1variable4 // starts with a number let #variable5 // starts with a special character other than dollar sign let break // starts with a JavaScript reserved keyword } |
Code Explanation
The code snippet above demonstrates the usage of valid and invalid variable names in Apps Script. The function myFunction()
serves as an example context for declaring variables.
Example
Let’s assume we want to declare a variable to store the user’s name. We can use the following code:
1 2 3 4 5 6 |
function greetUser() { const userName = "John Doe"; Logger.log("Hello, " + userName + "!"); } |
In this example, the variable name userName
is a valid choice as it starts with an alphabet and follows the camel case convention.
AI Prompt
Write a blog function about variable naming in Apps Script and include examples of both valid and invalid variable names.