--- title: console.assert() slug: Web/API/Console/assert tags: - API - DOM - Method - Reference - console translation_of: Web/API/console/assert ---
console.assert() 메서드는 주어진 가정이 거짓인 경우 콘솔에 오류 메시지를 출력합니다. 참인 경우, 아무것도 하지 않습니다.
{{AvailableInWorkers}}
console.assert(assertion, obj1 [, obj2, ..., objN]); console.assert(assertion, msg [, subst1, ..., substN]); // c-like message formatting
assertionobj1 ... objNmsgsubst1 ... substNmsg 매개변수의 치환 문자열에 대입할 JavaScript 객체.다음 예제는 객체와 가정을 함께 사용하는 법을 보입니다.
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"}
참고로, {{domxref("console.log()")}}의 치환 문자열을 거의 모든 브라우저에서 정상 동작하지만...
console.log('the word is %s', 'foo');
// output: the word is foo
console.assert()의 치환 문자열은 일부 브라우저에서 동작하지 않습니다.
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
{{domxref("console")}} 문서의 콘솔에 텍스트 출력하기 항목도 참고하세요.
| Specification | Status | Comment |
|---|---|---|
| {{SpecName("Console API", "#assert", "console.assert()")}} | {{Spec2("Console API")}} | Initial definition |
{{Compat("api.Console.assert")}}