Chrome/Firefox console.log always appends a line saying 'undefined'

前端 未结 7 1645
时光说笑
时光说笑 2020-11-21 13:40

Every time console.log is executed, a line saying undefined is appended to the output log.

It happens in both Firefox and Chrome on Windows

7条回答
  •  难免孤独
    2020-11-21 14:21

    Console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value.

    The result of an assignment expression is the value that was assigned. So the JavaScript engine just does an assignment but the console does one extra step which is to set whatever my last statement is, give you that value back. That’s why it prints 2:

    here.

    In statements that have no return value you get something like undefined.

提交回复
热议问题