Home  >  Article  >  Web Front-end  >  HTML DOM console.error() method

HTML DOM console.error() method

WBOY
WBOYforward
2023-09-12 14:01:011334browse

HTML DOM The console.error() method is used to write error messages to the console. This method is great for testing and debugging.

Syntax

The syntax of the console.error() method is as follows:

console.error(console.error(message))

Here, message is a JavaScript string or object. It is a required parameter value.

Example

Let’s see an example about the HTML DOM console.error() method −

<!DOCTYPE html>
<html>
<body>
<h1>console.error() Method</h1>
<p>Click the below button to write object as error message on the console</p>
<button type="button" onclick="errMessage()">ERROR</button>
<script>
   function errMessage(){
      var errObj = { Message:"ERROR has been caused",Value:"NEGATIVE"};
      console.error(errObj);
   }
</script>
<p>Press F12 key to view the error message in the console </p>
</body>
</html>

Output

This will produce the following output−

HTML DOM console.error() 方法

Click the ERROR button and view the Console tab in Developer Tools−

HTML DOM console.error() 方法

In the example above−

We first created a button named ERROR. When the user clicks, the errMessage() function will be executed −

<button type="button" onclick="errMessage()">ERROR</button>

The errMessage() method creates a button with members Message and objects of Value and their respective values. This object is then passed as a parameter to the error() method of the console object. The console's console.error() method prints the object as an error message to the console −

function errMessage(){
   var errObj = { Message:"ERROR has been caused",Value:"NEGATIVE"};
   console.error(errObj);
}

The above is the detailed content of HTML DOM console.error() method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete