📜  Node.js console.error()函数

📅  最后修改于: 2022-05-13 01:56:28.374000             🧑  作者: Mango

Node.js console.error()函数

Node.js 的控制台类中的console.error()函数用于在控制台上显示错误消息。它使用换行符打印到标准错误。
句法:

console.error([data][, ...args])

参数:该函数可以包含多个参数。第一个参数用于主消息,其他参数用于替换值。
返回值:函数返回错误信息。
下面的程序演示了 console.error()函数的工作:
方案一:

javascript
// Store number to variable
num = 20
  
// Check condition
if (num < 100) {
    console.log("Enter number greater then 100");
}
else {
    console.error("correct choice");
}


javascript
// Store number to variable
x = 20;
y = 50;
  
// Check condition
if (x > y) {
    console.error('%d is greater then %d', x, y);
}
else {
    console.error('%d is less then or equal to %d', x, y);
}


输出:

Enter number greater then 100

方案二:

javascript

// Store number to variable
x = 20;
y = 50;
  
// Check condition
if (x > y) {
    console.error('%d is greater then %d', x, y);
}
else {
    console.error('%d is less then or equal to %d', x, y);
}

输出:

20 is less then or equal to 50