This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by john
about 10 years

What is the return means in javascript?

so.. I googled it still can’t understand what’s the of return, what’s the meaning of it, what happens when I don’t use the return? And what’s the difference between console.log and return?

Answer 52d2ae3b52f8634c9b002848

1 vote

Permalink

Hi John, these are real important questions.

return is a mechanism that belongs to functions. It does not work outside of functions and if you try using it somewhere else you will get an error.

Every function has a return even if you don’t program it it still returns.

Why?

A function is a closed box. Inside it contains a script that performs some functionality, hence the name function.

Being a closed box, a function has an input mechanism that interfaces with the outside world: the parameters you see within the parentheses.

In order to be useful, a function must also have an output where it will give back, or display, or both , the results of its processing.

The official output of a function is the return mechanism.

So what does a return do? it outputs the processed data from a function and ends the function activity for that particular execution context.

This output could be displayed on the console (like we see at Codecademy), but normally this output is raw data that can be further processed by another script.


What is console.log?

log() is a mechanism that works with consoles.

console.log() is a command that means “console, log this data!

log(), or console.log() as we know it is not JavaScript. It is a proprietary mechanism of the browser consoles.

It was designed to facilitate testing instead of using those ugly alert() popup messages we used to get in the past.

In production console.log is not used. There, one must return the data or use other methods to display it such as confirm, prompt, window.document.write and many others.

Hope it helps

p.s. console.log vs return

console.log display something on a screen and does nothing else.

return can display something on the screen just like console.log, but it also delivers raw data that we can use somewhere else.

All output to screen comes in the form of string.


When doing the exercises at Codecademy follow these rules:

When they say return x it means to use the return keyword (make sure you are coding it inside of a function).

When they say print x it means to console.log it.

points
Submitted by tony de araujo
about 10 years