Console.log and calling a function questions

Hi everyone,

Should console.log always ben inside of a block of code? What about the code to call a function? Should that be inside or outside? Why?


anything inside your javascript file (or inside the <script></script> tags in the HTML file) is a good place to write your code! It can be in the first line, the last or anywhere else. For example:

// say hello to the user
console.log("Hello and welcome!");

// write a function to say hello
function sayHello() {
  console.log("Hello and welcome inside this function!");
sayHello() //you need to call a function for it to run

So it really depends on what you are trying to do!.

If you think about functions as pieces of code that you can reuse when you need them, it makes sense that you would call the functions from outside of its declaration. Functions that call themselves are called recursive functions

Thank you! It makes sense to me now :slight_smile:

1 Like