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

例如,像这样:

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

当前回答

异常被记录到JavaScript控制台。如果你想禁用Firebug,你可以使用它。

function log(msg) {
    setTimeout(function() {
        throw new Error(msg);
    }, 0);
}

用法:

log('Hello World');
log('another 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

在《调试JavaScript:扔掉你的提醒!》中概述了一种跨浏览器的好方法。

要真正回答这个问题:

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。

console.log("your message here");

为我工作…我正在找这个..我用火狐浏览器。 这是我的脚本。

 $('document').ready(function() {
console.log('all images are loaded');
});

适用于火狐和Chrome浏览器。

WebKit Web Inspector还支持Firebug的控制台API(只是Dan回答的一个小补充)。