console.assert()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
console.assert() は、検査結果が false になった場合に、コンソールへエラーメッセージを出力します。検査結果が true になる場合は何も行いません。
メモ: この機能はウェブワーカー内で利用可能です。
構文
js
assert(assertion, obj1); assert(assertion, obj1, obj2); assert(assertion, obj1, obj2, /* … ,*/ objN); assert(assertion, msg); assert(assertion, msg, subst1); assert(assertion, msg, subst1, /* … ,*/ substN); 引数
assertion-
任意の論理式です。結果が false になると、コンソールにメッセージを出力します。
obj1…objN-
出力する JavaScript オブジェクトのリスト。各オブジェクトを文字列で表現したものを、リストの並び順に追記して出力します。
msg-
0 個以上の置換文字列を含む JavaScript 文字列。
subst1…substN-
msg内の置換文字列を置き換える JavaScript オブジェクト。この引数で、出力形式を高度に制御できます。
返値
なし (undefined)。
例
次のコード例は、アサーションに続く JavaScript オブジェクトの使用を示しています。
js
const errorMsg = "the # is not even"; for (let number = 2; number <= 5; number++) { console.log(`the # is ${number}`); 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"} 詳しくは console のドキュメントで、コンソールへのテキストの出力をご覧ください。
仕様書
| Specification |
|---|
| Console> # assert> |