Z
Z
zlodiak2018-01-16 10:46:11
Node.js
zlodiak, 2018-01-16 10:46:11

Why does the nodejs console output undefined after the result?

Installed nodejs 8.9.4 . And I'm trying to add the numbers:

[email protected] ~ $ node
> 1 + 2
3

All OK. But when I try to display the result of console.log, then in addition to the result, undefined is also displayed:
> console.log('repl');
repl
undefined

Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-01-16
@zlodiak

Console features. This means that the expression does not return anything.
Try entering the following information:

function foo(x) { return x + 1 }
foo(2)

Result:
function foo(x) { return x + 1 }
undefined // объявление функции ничего не возвращает, хотя Function Declaration еще как!
         // консоль же интерпретирует это выражение как Function Expression
foo(2)
3 // вызов a возвращает значение 3

(function bar(x) { return x * x })  // объявление функции, обернутое в скобки,
(function bar(x) { return x * x }) //  интерпретируется как Function Declaration

Example with arrow function :
(x) => x + 1
(x) => x + 1 // объявление arrow function возвращает саму функцию

The call to console.log sends a string constructed from the arguments to standard output, but does not return any value. Therefore, you see the result of its execution and undefined .
Demo: https://jsfiddle.net/1qwaq2m0/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question