Need help understanding Reusable JavaScript with Functions

Need help understanding Reusable JavaScript with Functions
0

#1

Tell us what’s happening:
In the information box it says. You can call or invoke this function by using its name followed by parentheses, like this:

functionName();

I still wonder why I have to write reusableFunction(); at the end. Is it just to call the function? And why can’t it be inside the curly braces?

Your code so far
function reusableFunction () {
console.log(“Hi World”);

}

reusableFunction();


// Example
function ourReusableFunction() {
  console.log("Heyya, World");
}

ourReusableFunction();

// Only change code below this line
function reusableFunction () {
 console.log("Hi World");

}

reusableFunction();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions/


#2

This:

function reusableFunction () {
 console.log("Hi World");
}

simply defines what the function is. It doesn’t actually run it. Nothing actually happens from this code until it is called.

This:

reusableFunction();

actually calls and runs it.


#3

It if were inside the curly braces, it would be part of the function and wouldn’t be called. There are situations like that where a function calls itself from inside itself, called recursion - you’ll learn about that later. But you would still need a call outside the function to actually start the function in the first place.


#4

Thanks for the reply i understand now