본문 바로가기

JavaScriptVeryBeiginning

What's the difference between return and console.log?

함수 바깥으로 결과 가져오기.

console.log는 단순 출력(print)만 하기 때문에 return 이라는 키워드를 사용해 출력 결과를 가져올 수 있어야한다.

함수 안에 console.log만 있다면 그 함수는 결과값이 없는 함수가 된다.

 

const checkThatTwoPlusTwoEqualsFourAMillionTimes = () => {

  for(let i = 1; i <= 1000000; i++) {

    if ( (2 + 2) != 4) {

    console.log('Something has gone very wrong :( '); // Q1. so this console.log is not the same as return keyword? NO

    }

  }

};

 

const is2p2 = checkThatTwoPlusTwoEqualsFourAMillionTimes;

 

is2p2(); // Q2. this invokes the function, but doesn't give the return value? BECAUSE the function checkThatTwoPlusTwoEqualsFourAMillionTimes doens't have any "return" values.

 

console.log(is2p2.name); // Q3. How does this print the original function name? CONSOLE.LOG simply "prints" anything in it. Because of the name property, ".name" returns the name of a function