Learning JavaScript, Day 18 – Invoking Functions
I've spent this morning going over all the odds and ends for my MSc and...oof. Like, it's going to be good and it's definitely going to give me the language to say "Naw, that ain't happening" in proper business speak, but, ooof. There's going to be a lot of wandering through the long grass, hoping that the raptors aren't following...
Not to mention trying to remember how to write academically again. It's been a long time, and I'm going to have to remember to not keep on using really random analogies and cite every dang thing I do properly.
But, hey, right now? JavaScript Functions. Let's do this.
Invoking Functions
I am far too amused that rather than "call" or "start", they went with "invoke".
🔥 INVOKE THE JAVASCRIPT FUNCTION 🔥
You can build a function
function depecheMode() {
return "I just can't get enough";
}
And forget to call it.
You can build a function and invoke it
function depecheMode() {
return "Enjoy the silence";
}
depecheMode();
And forget to use the results of that invocation.
If your function returns a value, you can put it into a variable
function depecheMode() {
return "I feel you";
}
let track1 = depecheMode();
Which you can then use in document.
function depecheMode() {
return "I feel you";
}
let track1 = depecheMode();
document.getElementById("demo").innerHTML = track1;
I feel you
Or you can just display the returned value itself.
function depecheMode() {
return "Everything counts in small amounts";
}
document.getElementById("demo").innerHTML = depecheMode();
Everything counts in small amounts
You can invoke the function as many times as you need it
function depecheMode() {
return "Master and Servant";
}
let chorus1 = depecheMode();
let chorus2 = depecheMode();
let chorus3 = depecheMode();
document.getElementById("demo").innerHTML =
"Let's play " + chorus1 + "<br>" +
"Come on " + chorus2 + "<br>" +
chorus3;
Let's play Master and Servant
Come on Master and Servant
Master and Servant
() invokes the function, and whatever you put in between those parentheses will be used as a value.
function depecheMode(word) {
return "Let me take you on a " + word;
}
let lyrics = depecheMode(ship);
document.getElementById("demo").innerHTML = lyrics +
"<br>On a long long trip";
Let me take you on a ship
On a long long trip
But if you leave off the (), it just returns the function itself.
function depecheMode() {
return "Let me show you the world in my eyes";
}
document.getElementById("demo").innerHTML = depecheMode;
function depecheMode() {
return "Let me show you the world in my eyes"; }
You can stick functions anywhere, and they can be called from other functions or from any other code block.
function depecheMode(lyric) {
return "It's a question of " + lyric;
}
function ofTime() {
document.getElementById("demo").innerHTML =
depecheMode("time");
}
function ofLust() {
document.getElementById("demo2").innerHTML =
depecheMode("lust");
}
It's a question of time
It's a question of lust
(Is it obvious I'm listening to Depeche Mode while I'm doing this? You know me, I just can't get enough...)
Day 18 — Results
- You need to invoke a function to see the results of it.
- You can put your function returns into variables or just load it up as-is.
- You can invoke a function as many times as you need to.
- Anything you put between the
()will be used as a value when you invoke the function. - If you leave off the
()though, it just returns the whole function, not the value. - You can invoke functions in other functions or other code blocks.
Yeah, another fairly mellow day. Aside from having spent my morning being swamped in "Academic Integrity" and "Consent in Context" and "Information Systems Failures and the Technology Horizon", I also took an allergy pill in an attempt to stop hay fever, and I am so sleepy.

I'm so sleepy, Audrey.
Today's Sticker

Another Club Shifty delight — every envelope has at least one "grocery counter" type of sticker. Whether it's "fresh", "sliced", "family pack", "hot cheese", or "try me", you can be sure of something that is hilarious to inappropriately stick somewhere.