Let’s dig into these methods one by one.
Method 1: console.log() Method
The syntax of this method is described below:
In this code, the console.log() method is employed to print a message in the console window.
The output shows that two statements that were used with the console.log() method are printed on the console.
Method 2: document.write() Method
The message parameter passed to this method will be printed on the console.
For better understanding of this method, let’s have a look at the following example code:
In the above code, the document.write() method is used inside the <script> tags.
Method 3: inner.HTML Property
The inner.HTML property is famous for printing a message in an HTML document. In the method, the unique identification of the element is passed to get the HTML element in the document.
The description of the parameters is given below:
- getElementById: identify the unique id of the HTML element.
The example code for the innerHTML property is provided below.
The description of the code is as follows.
- A general message is displayed by using the heading <h2> tags.
- After that, a unique identification “p” is assigned to the paragraph element.
Method 4: window.alert() Method
Another approach to printing a message is possible through the window.alert() method. It is useful for form validation and confirmation of any user action in the browser. The syntax of the window.alert() method is as follows:
The message argument will be displayed as an alert message.
In this code:
- A message is displayed using the heading <h2> tag.
- After that, the alert() method is employed within the <script> tags.
|clear()||console.clear()||This method clears the console.|
|error()||console.error()||Prints an error message.|
|info()||console.info()||Prints informational messages.|
|warn()||console.warn()||Displays a warning on the console.|
|assert()||console.assert()||Error message if and only if the assertion is false.|