console.assert() method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.

注意: 此特征可用于 Web 工作者 .

句法

console.assert(assertion, obj1 [, obj2, ..., objN]);
console.assert(assertion, msg [, subst1, ..., substN]); // C-like message formatting
					

参数

assertion

Any boolean expression. If the assertion is false, the message is written to the console.

obj1 ... objN

A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

msg

A JavaScript string containing zero or more substitution strings.

subst1 ... substN
JavaScript objects with which to replace substitution strings within msg . This parameter gives you additional control over the format of the output.

范例

The following code example demonstrates the use of a JavaScript object following the assertion:

const errorMsg = 'the # is not even';
for (let number = 2; number <= 5; number += 1) {
    console.log('the # is ' + number);
    console.assert(number % 2 === 0, {number: number, errorMsg: errorMsg});
    // or, using ES2015 object property shorthand:
    // console.assert(number % 2 === 0, {number, errorMsg});
}
// output:
// the # is 2
// the # is 3
// Assertion failed: {number: 3, errorMsg: "the # is not even"}
// the # is 4
// the # is 5
// Assertion failed: {number: 5, errorMsg: "the # is not even"}
					

Note that, while a string containing a substitution string works as a parameter for console.log in Node and many, if not most, browsers...

console.log('the word is %s', 'foo');
// output: the word is foo
					

...the use of such a string does not currently work as intended as a parameter for console.assert in all browsers:

console.assert(false, 'the word is %s', 'foo');
// correct output in Node.js and some browsers
//     (e.g. Firefox v60.0.2):
// Assertion failed: the word is foo
// incorrect output in some browsers
//     (e.g. Chrome v67.0.3396.87):
// Assertion failed: the word is %s foo
					

把文本输出到控制台 在文档编制的 console 进一步了解细节。

规范

规范 状态 注释
控制台 API
The definition of 'console.assert()' in that specification.
实时标准 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
assert Chrome 1 Edge 12 Firefox 28 IE 8 Opera 11 Safari 4 WebView Android 1 Chrome Android 18 Firefox Android 28 Opera Android 11 Safari iOS 3.2 Samsung Internet Android 1.0

图例

完整支持

完整支持

元数据

  • 最后修改: