Functions
List all the functions you already know (hint: anything with
()at the end)prompt,console.log,document.getElementById,parseInt,array.toUpperCase
Part of the magic of programming is reuse! You can write your own functions and then use them many times.
function addKitten() {
const container = document.getElementById("container");
const kittenImg = document.createElement("img");
kittenImg.src = "http://placekitten.com/g/200/300";
container.appendChild(kittenImg);
}
addKitten();
addKitten();
addKitten();What happens if...?
I don't call the function at all?
I forget the
()?I call the function before defining it?
Parameters and return values
Function parameters + return values
Extend this example:
If no parameter is passed in, return
ARRRGHHH!!!instead.
Let's do another example:
What happens if...
I don't have a
returnat all in the function?I try to
console.log(num1)outside the function? After the function? After the function is called?What if I use
constorletto create a variable inside the function?How many parameters can I have?
What happens if I put some code after the
return?How do I return multiple bits of information?
References
Last updated
Was this helpful?