console.assert()

console.assert() は、検査結果が false になった場合に、コンソールへエラーメッセージを出力します。検査結果が true になる場合は何も行いません。

注: この機能は Web Worker 内で利用可能です。

構文

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 になると、コンソールにメッセージを出力します。

obj1objN

出力する JavaScript オブジェクトのリスト。各オブジェクトを文字列で表現したものを、リストの並び順に追記して出力します。

msg

0 個以上の置換文字列を含む JavaScript 文字列。

subst1substN

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 Standard
# assert

ブラウザーの互換性

BCD tables only load in the browser