Difference between console.log and sys.puts in node.js?

后端 未结 7 1744
無奈伤痛
無奈伤痛 2021-01-31 01:38

In node.js you can use console.log or sys.puts to print out to the screen.

What is the preferred method and what is the difference between thes

相关标签:
7条回答
  • 2021-01-31 02:09

    Puts is deprecated in since version 0.2.3

    0 讨论(0)
  • 2021-01-31 02:20
    console.dir(objectToInspect)
    

    This might be another way to inspect objects.

    0 讨论(0)
  • 2021-01-31 02:24

    Also you can use console.log without requiring the sys module.

    0 讨论(0)
  • 2021-01-31 02:28

    Both just write to the stdout stream. The difference is that sys.puts just toString's the first argument, and console.log takes multiple arguments, and will sys.inspect the first arg if it's not a string.

    0 讨论(0)
  • 2021-01-31 02:29
    sys.puts([...]);
    

    Is a synchronous output function. Simply it is a blocking function as in Node.js language.

    console.log([data], [...]);
    

    Prints to stdout with newline.

    For more Info:

    http://nodejs.org/api/stdio.html#stdio_console_log_data

    http://nodejs.org/api/util.html#util_util_puts

    Note: 'sys' module was renamed to be 'util' (Supported by link) It was a Fix #3577

    0 讨论(0)
  • 2021-01-31 02:30

    The preferred method is console.log(). sys.puts() has been removed.


    The sys module was renamed util with this commit dated Oct 12, 2010. So sys.puts() became util.puts(). util.puts() was added in v0.3.0, deprecation since v0.11.3, and removed in v12.0.0. The documentation advises to use console.log() instead.

    0 讨论(0)
提交回复
热议问题