Functions in JavaScript

A function is a block of reusable code. It
is useful because you can execute it many times. To define a function, we use var like we did with declaring a variable.

You'll see on line 1, we define a function named hello. What does this function do? Line 3 tells us that it uses console.log to print "i am saying hello".

But nothing happens if you stop here. You have to call the function. On line 6, you'll see we call the function hello. This asks the program to run all the code in the hello function (ie. the code between the curly { } brackets).

So calling the hello function once would result in "i am saying hello" being printed out once in the console.


Make the console show "i am saying hello" twice.

Stuck? Get a hint! Hint
Get live, fast support from Codecademy Advisors when you get stuck. Learn more.

Look at the calling of the function hello on line 6. Maybe calling it once again could work?

Remember, the code that is after // on line 2 are just comments we use to help us keep track of what our code is doing. The computer ignores this so it does not affect how our code runs.