Node.js console.clear() 方法用于清除 stdout(标准输出),如果是 TTY(Teletype),调用 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("qikepu");
var TP = "easy learning"
console.log("Simply %s at your fingertips", TP);
console.clear();
输出
执行后,它将打印以下输出 -
qikepu
Simply easy learning at your fingertips
Simply easy learning at your fingertips
为了清楚地理解这一点,让我们在浏览器的控制台中查看上述程序的执行情况。

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

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