{"id":15309,"date":"2023-07-24T09:00:00","date_gmt":"2023-07-24T00:00:00","guid":{"rendered":"https:\/\/www.autooffice.co.kr\/blog\/2023\/07\/24\/understanding-variables-in-apps-script\/"},"modified":"2025-09-15T18:13:30","modified_gmt":"2025-09-15T09:13:30","slug":"understanding-variables-in-apps-script","status":"publish","type":"post","link":"https:\/\/www.autooffice.co.kr\/en\/blog\/2023\/07\/24\/understanding-variables-in-apps-script\/","title":{"rendered":"Understanding Variables in Apps Script"},"content":{"rendered":"\n<p>This post is a corrected post after AI explains the code included in the book <a href=\"https:\/\/www.autooffice.io\/content\/ebizbooks-apps-script\" target=\"_blank\" rel=\"noopener\" title=\"\">&#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\n\n<h2>Summary<\/h2>\n<p>In this blog post, we will explore the concept of variables in Apps Script. We will discuss how to declare and assign values to variables, and how their values can be changed throughout the script.<\/p>\n<h2>Table of Contents<\/h2>\n<ul>\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=\"#prompt\">AI Prompt<\/a><\/li>\n<\/ul>\n<h2 id=\"code\">Code<\/h2>\n<pre>function variables(){\n  var value = 1\n  Logger.log(value)\n\n  var value = 2\n  Logger.log(value)\n\n  Logger.log(value)\n}\n    <\/pre>\n<h2 id=\"explanation\">Code Explanation<\/h2>\n<p>In the given code, we have a function named &#8220;variables&#8221; that contains three lines of code.<\/p>\n<p><span style=\"color: #ff3366;\">(It&#8217;s actually 5 lines of code, but the AI seems to recognize them as 3 blocks.)<\/span><\/p>\n<ul>\n<li>First, we declare a variable named &#8220;value&#8221; and assign it a value of 1.<\/li>\n<li>Then, we declare another variable with the same name &#8220;value&#8221; and assign it a value of 2. This reassignment does not cause an error and the variable&#8217;s value is updated.<\/li>\n<li>Finally, we log the value of the variable to the logger. Here, the value will be 2, as it was reassigned in the previous line.<\/li>\n<\/ul>\n<h2 id=\"example\">Example<\/h2>\n<p>Let&#8217;s run the &#8220;variables&#8221; function and see the output in the logger:<\/p>\n<pre>variables()\n    <\/pre>\n<h2 id=\"prompt\">AI Prompt<\/h2>\n<p>Write <span style=\"color: #ff3366;\">Apps Script<\/span> function that declares a variable &#8220;value&#8221; and assigns it a value of 1. Then, reassign the variable with the same name &#8220;value&#8221; and assign it a value of 2. Finally, log the value of the variable to the logger.<\/p>","protected":false},"excerpt":{"rendered":"<p>In this blog post, we will explore the concept of variables in Apps Script. We will discuss how to declare and assign values to variables, and how their values can be changed throughout the script.<\/p>\n","protected":false},"author":1,"featured_media":15305,"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,7,518],"class_list":["post-15309","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spreadsheet","category-apps-script","tag-ai-assisted","tag-script","tag-spreadsheet","tag-variables"],"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\/15309","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=15309"}],"version-history":[{"count":4,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15309\/revisions"}],"predecessor-version":[{"id":17204,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/posts\/15309\/revisions\/17204"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media\/15305"}],"wp:attachment":[{"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/media?parent=15309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/categories?post=15309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.autooffice.co.kr\/en\/wp-json\/wp\/v2\/tags?post=15309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}