console.assert()
메서드는 주어진 가정이 거짓인 경우 콘솔에 오류 메시지를 출력합니다. 참인 경우, 아무것도 하지 않습니다.
주의:
이 기능은 Web Worker에서 사용할 수 있습니다.구문
console.assert(assertion, obj1 [, obj2, ..., objN]); console.assert(assertion, msg [, subst1, ..., substN]); // c-like message formatting
Parameters
assertion
- 아무 불리언 표현식. 거짓인 경우, 메시지를 콘솔에 출력합니다.
obj1
...objN
- 출력에 사용할 JavaScript 객체. 각각의 문자열 표현을 순서대로 출력합니다.
msg
- 0개 이상의 치환 문자열을 포함하는 JavaScript 문자열.
subst1
...substN
msg
매개변수의 치환 문자열에 대입할 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"}
참고로, 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
console
문서의 콘솔에 텍스트 출력하기 항목도 참고하세요.
명세
Specification | Status | Comment |
---|---|---|
Console API The definition of 'console.assert()' in that specification. |
Living Standard | Initial definition |
브라우저 호환성
BCD tables only load in the browser