• Node.js Video Tutorials

NodeJS - console.clear() 方法



Node.js 的 console.clear() 方法用于清除 stdout(标准输出),如果它是 TTYTeletype),则调用 console.clear() 会尝试清除 TTY。当 stdout 不是 TTY 时,此方法将不执行任何操作。

console.clear() 方法的具体操作可能因不同的操作系统和终端类型而异。

  • 在 Linux 操作系统中,console.clear() 方法的操作类似于终端中的 clear shell 命令。

  • 在 Windows 上,console.clear() 方法仅清除 Node.js 二进制文件当前终端视口中的输出。

语法

以下是 Node.js console.clear() 方法的语法:

console.clear()

参数

此方法不接受任何参数。

返回值

此方法不返回任何内容,而是清除控制台。

示例

在浏览器的 检查器 窗口(如 google chrome、edge 等)中执行以下程序以查看结果。

console.log("Tutorialspoint");
var TP = "easy learning"
console.log("Simply %s at your fingertips", TP);
console.clear();

输出

执行后,它将打印以下输出:

Tutorialspoint
Simply easy learning at your fingertips

为了更清楚地理解这一点,让我们看看上述程序在浏览器控制台中的执行情况。

console_browser

我们调用了 console.clear() 方法。

console_clear

在终端中调用 console.clear() 方法后,它将清除控制台屏幕,如下面的图所示。

console_screen
nodejs_console_module.htm
广告