Hoisting issues in declaring variables then use them within each other in Google Script?

javascript closure
es6 hoisting
var vs let
javascript let
javascript nested functions
hoisting in javascript mdn
javascript variable scope
javascript global variable

I've declared two variables (link1 and link2) and use them within another declared variable (message). Then I created an if-statement where I use 'message' but I want to change the value of the two variables within the if-statement. In the end, I get a wrong output.

var link1, link2;
var messageFailed = "undefined";
var message = "Your first link: " + link1 + "Your second link: " + link2

if(true){
link1 = "Hello"
link2 = "World"
MailApp.sendEmail(message);
}
MailApp.sendEmail(messageFailed);

I can simply re-declare the 'message' variable within the if-statement, but I plan on using multiple if-statements so the code will end up looking really messy and a pain to change the 'message' text if needed. How would I go about changing the 'link1' and 'link2' variables after the 'message' variable is already declared?

How would I go about changing the 'link1' and 'link2' variables after the 'message' variable is already declared?

You can change link1 and link2 but it will never affect the value of message, because you cannot go back in time. The string concatenation has already happened.

Instead you can create a function that generates the message based on the parameters it get passed:

function createMessage(link1, link2) {
  return "Your first link: " + link1 + "Your second link: " + link2
}

if(true){
  MailApp.sendEmail(createMessage("Hello", "World"));
}

That's how code reuse works. You are moving the parts that are fixed into a function and make the parts that are variable parameters of that function.

Javascript hoisting in action, means that variable and function declarations are processed before any code is executed. In JavaScript, there are three keywords used to declare a variable — var, let, and const — and each one affects how the code will interpret the variable differently. This tutorial will cover what variables are, how to declare and name them, and also take a closer look at the difference between var , let , and const .

So as I mentioned in a comment, you cannot because it is assigned to a value. What you could do though is write a super tiny helper function to format it.

var link1 = "", link2 = "";
var message = () => {
  return "Your first link: " + link1 + " Your second link: " + link2;
}

if(true) {
    link1 = "waffles";
    link2 = "juicyfruit";
    console.log(message());
}

Declaring JavaScript Variables: var, let and const ― Scotch.io, are declared, they are moved to the top of their scope regardless of whether their scope is global or local. A guide to JavaScript variable hoisting ? with let and const New JavaScript developers often have a hard time understanding the unique behaviour of variable/function hoisting . Since we’re going to be talking about var , let and const declarations later on, it’s important to understand variable hoisting rather than function hoisting .

When you assign message you're calculating your result and assigning it then. Changing those variables afterwards does not change the string that was calculated when you assigned message. You need to create your message after you finish changing your variables.

Understanding Hoisting in JavaScript ― Scotch.io, A quick and painless description of JavaScript Hoisting. you were able to call functions before you wrote them in your code, then read on! As we mentioned before, all variable and function declarations are hoisted to the top of their scope. Because of this, we can use variables before we declare them. The other approach to this, more often seen in scripting languages, is that if a variable is used without having been declared it is instantiated at first use. This can make it somewhat hard to follow the flow of code and is often used as a criticism of languages that behave that way.

There's no need to declare all javascript vars once at the top of a , So, the convention that variables should be declared at their first use is learn to spot variable hoisting issues before they happen, rather than  A mostly reasonable approach to declare JavaScript Variables . Always declare variables in a consistent manner, and at the top of their scope. Keeping variable declarations to one per line is encouraged. Comma-first, a single var statement, multiple var statements and be consistent across the project.

JavaScript Fundamentals: Variables, Scope & Hoisting, JavaScript variables are containers for storing data values. Open in app We'll take a look at how variables behave in our code, with hoisting and scope Here we've declared our variable using var , our identifier is name , we've then It will return a ReferenceError and halt the execution of the script. Using JavaScript operations within your script is considerably faster than calling other services. Anything you can accomplish within Google Apps Script itself will be much faster than making calls that need to fetch data from Google's servers or an external server, such as requests to Spreadsheets, Docs, Sites, Translate, UrlFetch, and so on.

Client-Server Web Apps with JavaScript and Java: Rich, Scalable, , The primary trait of JavaScript that makes it like Scheme, as it relates to both its origin JavaScript has function-level scope rather than block-level scope like other or variable might be hoisted or moved to top of scope where it is defined. the issue is to use a single var statement at the top scope to declare all variables  0 Toggle multiple MaterializeCSS icons without using an id selector Feb 14 '19 0 Website forces user to click back button quickly and repeatedly to go back Feb 21 '19 0 Can you put multiple content items for a single meta og tag?

Comments
  • To put it simply, you can't. You have assigned the value of message and it retains that value. It doesn't update like a placeholder. You could write a helper function here though.
  • Try placing the if true block before the "var message" line. Another thing that may be useful to know down the road is "undefined" and undefined (without the quotes) have very different meanings in JavaScript.