-6

Whats the difference between console.log and return in JavaScript? they both seen to print out things in terminal.

  isPrime(num){
    if (num % i === 0)) {
     return false ;
   }
  for (var i = 2; i < num; i++) {
    if (num % i === 0) {
   return false;
    }
 }
  • `return` doesn't print anything. The function that gets the returned value is probably the one that prints the result!!! – ibrahim mahrir Feb 09 '17 at 01:46
  • The console ("terminal") will always display the result of the last expression. For example, `console.log("foo")` prints `foo` to console, then returns `undefined`, which is also printed by the console as the last evaluation. `console.log` will print stuff to console even from webpage's code, where evaluations are not printed. – Amadan Feb 09 '17 at 01:48

2 Answers2

1

Return

The return statement ends function execution and specifies a value to be returned to the function caller.

Console.log

The Console object provides access to the browser's debugging console (e.g., the Web Console in Firefox)

console.log Outputs a message to the Web Console under development tool concel tab.

Community
  • 1
  • 1
XY L
  • 25,431
  • 14
  • 84
  • 143
0

console.log() is meant to print output to the console. However, return is meant to send data back to variable when a function is called, like this:

var foo = bar();

If bar() had a return statement, the value would be passed to foo.

Leo Wilson
  • 503
  • 1
  • 5
  • 16