Ask coding questions

← Back to all posts
Short question
dtolik (0)

Replit wont give me an output for a very simple function:

function cube(x) {
return x x x;
}
cube(3)

It works in a console, but Replit won't give any output for this or any other basic math functions I am creating.

If I do it through console.log works fine as well.

The reason I had this question was that I had some JS 101 course through Zoom, and that original code I show before was giving output on the presenter Replit, but not on mine, it was so strange...

Any ideas why?

Comments
hotnewtop
SixBeeps (5066)

The Browser console is special, in that it shows you the output of a function if you decide to call something from there. The built-in Replit console doesn't do this, so if you absolutely need this kind of functionality, you'll have to stick with a console.log call.

tussiez (1533)

Do Something

You must do something after the code is run for you to see results.
For example, you could use console.log.

function cube(x) {
return x x x;
}
console.log(cube(3))
RYANTADIPARTHI (6011)

Solution

you need to use console.log to print your output to the console. OR maybe document.write to print to the web page.

function cube(x) {
  return x x x;
}
console.log(cube(3));

or

function cube(x) {
  return x x x;
}
document.write(cube(3));

like that.
That should work

dtolik (0)

@RYANTADIPARTHI Thank you!
I understand. I could also do something like that:
function fullName(firstName, lastName) {
var result = (firstName + " " + lastName);
return result
}
var resultElement = fullName("John", "Doe") // => "John Doe"
console.log(resultElement)

The only reason I had this question was that I had some JS 101 course through Zoom, and that original code I show before was giving output on the presenter Riplit, but not on mine, it was so strange...

Thanks again to all of you!

RYANTADIPARTHI (6011)

@dtolik no problem. Click check mark please.

Coder100 (17144)

Repl.it will not show you the return statement of the last expression. That is a buggy node feature that replit accidentally has.

In the web context, dead code like that won't even be evaluated.

So you just have to use console.log

19wintersp (1120)

The Replit console in a HTML repl is based on the output through the Web Console API; it will only show things if you use any of the console methods such as log, error, info, and warn.