简体   繁体   中英

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

Say we have a javascript file called "test.js". 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). 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? In other words, if there is a method that can make Node.js to display result of statements in "Code 1" in console directly? (The "display-statement-result-directly" behavior like what PowerShell does)

You can use node.js' REPL session. In order to use it, you can open up a terminal and write "node" to enter a REPL (Read, Eval, Print, Loop) session immediately. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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