0

I stored the function in the variable "a" and I was expecting it to return "hello" in the html but it instead returned the whole function as a string.

When calling the function using the variable plus parantheses "a()" it outputted the string "hello" in the html, which is what I wanted to do. Can anyone explain why this works? Thank you :)

let a = function greetings() {
    let b = "hello";
    return b;
};

document.getElementById('hi').innerHTML = `This is the output without the parentheses: <br> ${a}
<br><br>
This is the output with the parentheses: <br> ${a()}`;
<p id='hi'>...</p>
Lain
  • 3,657
  • 1
  • 20
  • 27
  • `a` is a function and not the return of that function. You have to call it to get its return like you are doing correctly the second time `${a()}` or assigning its return to `b` using like `b = a()` and use `${b}` – Lain Dec 22 '22 at 07:41

0 Answers0