{"id":15334,"date":"2023-07-25T09:00:00","date_gmt":"2023-07-25T00:00:00","guid":{"rendered":"https:\/\/www.autooffice.co.kr\/blog\/2023\/07\/25\/handling-variables-in-apps-script\/"},"modified":"2025-09-15T18:16:24","modified_gmt":"2025-09-15T09:16:24","slug":"handling-variables-in-apps-script","status":"publish","type":"post","link":"https:\/\/www.autooffice.co.kr\/en\/blog\/2023\/07\/25\/handling-variables-in-apps-script\/","title":{"rendered":"Declare with let"},"content":{"rendered":"<p>This post is a corrected post after AI explains the code included in the book <a title=\"\" href=\"https:\/\/www.autooffice.io\/content\/ebizbooks-apps-script\" target=\"_blank\" rel=\"noopener\">&#8220;Google Apps Script 101: Building Work Automation For Free&#8221;<\/a>. Added a <s>strikethrough<\/s> when editing what AI has written, and added <span style=\"color: #ff3366;\">color<\/span> when edited by author<\/p>\n<h2>Table of Contents<\/h2>\n<ul>\n<li><a href=\"#summary\">Summary<\/a><\/li>\n<li><a href=\"#code\">Code<\/a><\/li>\n<li><a href=\"#explanation\">Code Explanation<\/a><\/li>\n<li><a href=\"#example\">Example<\/a><\/li>\n<li><a href=\"#ai-prompt\">AI Prompt<\/a><\/li>\n<\/ul>\n<p><!-- Summary --><\/p>\n<h2 id=\"summary\">Summary<\/h2>\n<p>In this blog post, we&#8217;ll dive into a simple Apps Script code snippet that demonstrates how to <span style=\"color: #ff3366;\">declare<\/span> <s>handle<\/s> variables <span style=\"color: #ff3366;\">with let <\/span>in JavaScript. Apps Script is a scripting language used for automating tasks in various Google Workspace applications, such as Google Sheets, Docs, and Forms. Understanding how to work with variables is essential for efficient and effective script development.<\/p>\n<p><!-- Code --><\/p>\n<h2 id=\"code\">Code<\/h2>\n<pre><code>\nfunction variables2() {\n  let value = 1;\n  Logger.log(value);\n\n  let value = 2;\n  Logger.log(value);\n}\n<\/code><\/pre>\n<p><!-- Code Explanation --><\/p>\n<h2 id=\"explanation\">Code Explanation<\/h2>\n<p>The provided code snippet represents a function named <code>variables2()<\/code>.<br \/>\nWithin this function, two variables with the same name, <code>value<\/code>, are declared using the <code>let<\/code><br \/>\nkeyword, which allows block-scoped variable declarations.<br \/>\nHowever, declaring two variables with the same name in the same scope is not allowed in JavaScript, which results in an error. To fix this, we should give each variable a unique name.<br \/>\nLet&#8217;s modify the code to demonstrate the proper way to handle variables.<\/p>\n<p><!-- Example --><\/p>\n<h2 id=\"example\">Example<\/h2>\n<p>Let&#8217;s consider a scenario where we want to track the count of items in a shopping cart using variables in Apps Script. We&#8217;ll create a function that updates the cart and another function to display the cart&#8217;s item count. Additionally, we&#8217;ll avoid re-declaring variables with the same name to prevent errors. Here&#8217;s the revised code:<\/p>\n<pre><code>\nlet cartItemCount = 0;\n\nfunction addToCart(itemCount) {\n  cartItemCount += itemCount;\n}\n\nfunction displayCartItemCount() {\n  Logger.log('Cart Item Count: ' + cartItemCount);\n}\n<\/code><\/pre>\n<p>Now, we have declared a single variable <code>cartItemCount<\/code> outside any function to make it accessible<br \/>\nacross functions. The <code>addToCart()<\/code> function takes an argument <code>itemCount<\/code> and adds it to<br \/>\nthe cart&#8217;s item count.<br \/>\nThe <code>displayCartItemCount()<\/code> function logs the cart&#8217;s item count to the console. This way, we can<br \/>\nefficiently manage the cart&#8217;s item count without any conflicts.<\/p>\n<p><span style=\"color: #ff3366;\">In this part, AI has created examples that are out of context, making it difficult for readers to understand. Rather, it would be better to present the code finally presented by AI below as a revision.<\/span><\/p>\n<pre><code>\nfunction variables2(){\n  let value = 1\n  Logger.log(value)\n\n  let newValue = 2\n  Logger.log(newValue)\n}\n<\/code><\/pre>\n<p><!-- AI Prompt --><\/p>\n<h2 id=\"ai-prompt\">AI Prompt<\/h2>\n<p><s>To generate the revised code snippet provided in this blog post, input the following AI prompt into an AI-based code generation tool:<\/s><\/p>\n<p><span style=\"color: #ff3366;\">Generate apps script a function named variables2 that logs two variables to the console. Declare a variable &#8216;value&#8217; and assign it the value 1. Then declare another variable &#8216;newValue&#8217; and assign it the value 2. Finally, log both variables.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post, we&#8217;ll dive into a simple Apps Script code snippet that demonstrates how to declare handle variables with let in JavaScript.<\/p>\n","protected":false},"author":1,"featured_media":15357,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[5,118],"tags":[512,69,519,7],"class_list":["post-15334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spreadsheet","category-apps-script","tag-ai-assisted","tag-script","tag-let","tag-spreadsheet"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","enabled_languages":["ko","en"],"languages":{"ko":{"title":true,"content":true,"excerpt":true},"en":{"title":true,"content":true,"excerpt":true}}},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/comments?post=15334"}],"version-history":[{"count":8,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15334\/revisions"}],"predecessor-version":[{"id":17207,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15334\/revisions\/17207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media\/15357"}],"wp:attachment":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media?parent=15334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/categories?post=15334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/tags?post=15334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}