Function Declarations - No display of executed program Result


#1

Hi Friends,

As show in the above image, i have written the code correctly for the function declaration and it is successfully compiled but still there is no display of any result like true or false for the given parameters for the function isGreaterThan in the display terminal ???

can anybody explain me what is error in my code or something ??


#2

if you want to display something, you need to log to console what you want to display (console.log())


#3

i taught it will return the boolean values like true or false instead of console.log() , now i will redit the code and see the result…


#4

yes, but return means literally that, the function hands you something back:

function example(){
    return "Hello world"
}

x = example()

now we have x variable which contains the returned result, which we could log to the console. Or call console.log directly on the function to log the returned result to the console directly.


#5

Now i got understood…Thanks a lot brother


#6

Is it good coding standard to return console.log inside your function block and that way you just call the function and pass it its parameters ? Is there any benefit in doing this? Maybe when you are writing a program that calls the function multiple times? Is there anything wrong with doing this?
Example:

function isGreaterThan (numberOne , numberTwo){
  if(numberOne > numberTwo){
    return console.log(true);
  }
  else {
    return console.log(false);
  }
  }
isGreaterThan(23, 5);

#7

no, this is a terrible idea. console.log() is a built-in function which returns undefined, so now we can’t capture the returned result by your isGreaterThan function, let me show you:

function isGreaterThan (numberOne , numberTwo){
  if(numberOne > numberTwo){
    return console.log(true);
  }
  else {
    return console.log(false);
  }
  }
x = isGreaterThan(23, 5);
console.log(x); // will display undefined, it should display true

you should really put the console.log and return on separate lines.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.