HTML DOM console.clear() 메서드는 콘솔을 지우는 데 사용됩니다. console.clear() 메서드는 콘솔에 "Console was cleared" 메시지를 작성하고 이전 콘솔 내용을 모두 지웁니다. 이 방법은 모든 브라우저에서 완벽하게 지원됩니다.
구문
다음은 console.clear() 메서드의 구문입니다. -
console.clear()
예시
HTML DOM console.clear() 메서드의 예를 살펴보겠습니다. -
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript console.clear() Method</h1>
<p>Press F12 on the keyboard to see the message on your console</p>
console.log("TEXT has been printed on the console!");
<p>Click the below button to clear the console</p>
<button onclick="clearConsole()">CLEAR</button>
<script>
function clearConsole() {
console.clear();
}
</script>
</body>
</html> 출력
이것은 다음과 같은 출력을 생성합니다 -

콘솔 -

CLEAR 버튼을 클릭하면 -

위의 예에서 -
console.log() 메서드를 사용하여 콘솔에 메시지를 작성했습니다 -
console.log("TEXT has been printed on the console!"); 그런 다음 clearConsole() 메서드를 실행할 CLEAR 버튼을 만들었습니다.
<button onclick="clearConsole()">CLEAR</button>
clearConsole() 메서드는 콘솔 객체에서 clear() 함수를 호출합니다. 이렇게 하면 콘솔이 지워지고 콘솔에 "콘솔이 지워졌습니다"라고 씁니다.
function clearConsole() {
console.clear();
}