简体   繁体   English

如何在 node.js 中不使用 console.log() 来显示语句的结果

[英]How to display result of statements without using console.log() in node.js

Say we have a javascript file called "test.js".假设我们有一个名为“test.js”的 javascript 文件。 The code in this file is:这个文件中的代码是:

// Code 1
let x = 1, y = 2;
x + y;
x * 2;

We can run it using Node test.js in console (eg CMD in Windows).我们可以在控制台中使用Node test.js运行它(例如 Windows 中的 CMD)。 But in this case, nothing will be displayed.但在这种情况下,什么都不会显示。 We have to modify the code to:我们必须将代码修改为:

// Code 2
let x = 1, y = 2;
console.log(x + y);
console.log(x * 2);

to let the following result to be displayed in console:让以下结果显示在控制台中:

3
2

The question is, if there is a method that can make Node.js to display result of statements in console without using the console.log() method?问题是,是否有一种方法可以让 Node.js 在使用console.log()方法的情况下在控制台中显示语句的结果? In other words, if there is a method that can make Node.js to display result of statements in "Code 1" in console directly?换句话说,是否有一种方法可以让Node.js直接在控制台中显示“代码1”中的语句结果? (The "display-statement-result-directly" behavior like what PowerShell does) (与 PowerShell 类似的“直接显示语句结果”行为)

You can use node.js' REPL session.您可以使用 node.js 的 REPL 会话。 In order to use it, you can open up a terminal and write "node" to enter a REPL (Read, Eval, Print, Loop) session immediately.为了使用它,您可以打开一个终端并编写“节点”以立即进入一个 REPL(读取、评估、打印、循环)会话。 One of its many benefits is that you can see expression results without logging them directly to the console.它的众多好处之一是您可以查看表达式结果,而无需将它们直接记录到控制台。 You can refer to the Documentation for more information.您可以参考文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM