JS Return statement is not working on VS code

Return statement isn’t working when I run any function. But if I run it with console.log operator then the function works properly. Why is it happening?
Here is two photos, one I run the function with console.log which is running okay but another one with return statement isn’t showing the result. Please give me solution, it’s been a while I am stuck at this.

we cannot give you the solution we can only guide you or help you

Why do you think the return statement is not working?

A return statement doesn’t log output to the console.

I think it’s a technical problem

I don’t think so. The return statement should be working as intended in the code you posted.

You can see when I use consol.log it’s showing 8, but when use return it’s showing nothing

Right. Because a return statement doesn’t log anything to the console. It isn’t supposed to log anything to the console. Only console.log logs things to the console. The return statement passes the return value back to the calling context.

So what am I doing wrong

You are not doing anything wrong. The code is working correctly.

If you want to show the answer in the console, then you need to use console.log. If you want to the function to return a value, then you must use a return statement. If you want to do both, the you need to do both.

Thank you very much. Now it’s all clear to me. :heart:

1 Like