我如何打印消息到错误控制台,最好包括一个变量?

例如,像这样:

print('x=%d', x);

当前回答

要真正回答这个问题:

console.error('An error occurred!');
console.error('An error occurred! ', 'My variable = ', myVar);
console.error('An error occurred! ' + 'My variable = ' + myVar);

除了error,你还可以使用info, log或warn。

其他回答

如果你使用Safari,你可以写

console.log("your message here");

它就出现在浏览器的控制台上。

安装Firebug,然后你就可以使用console.log(…)和console.debug(…)等等(更多信息请参阅文档)。

如果您正在使用Firebug,并且需要支持IE、Safari或Opera, Firebug Lite为这些浏览器添加了console.log()支持。

这不会打印到控制台,但会打开一个警告弹出窗口,其中包含你的消息,这可能对一些调试有用:

只做:

alert("message");
console.error(message); // Outputs an error message to the Web Console
console.log(message); // Outputs a message to the Web Console
console.warn(message); // Outputs a warning message to the Web Console
console.info(message); // Outputs an informational message to the Web Console. In some browsers it shows a small "i" in front of the message.

你也可以添加CSS:

console.log('%c My message here', "background: blue; color: white; padding-left:10px;");

更多信息可以在这里找到:https://developer.mozilla.org/en-US/docs/Web/API/console