Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript output


May 06, 2021 JavaScript


Table of contents


JavaScript output


JavaScript does not have any functions to print or output.

JavaScript can output data in different ways:

  • Use window.alert() to pop up the warning box.
  • Use the document.write() method to write content to html documents.
  • Write to HTML elements using innerHTML.
  • Use console .log () write to the browser's console.

Using window.alert()

You can pop up a warning box to display the data:

< ! DOCTYPE html >
< Html >
< body >

< h1 . . . my first page >
< My first paragraph. < /p >

< script >
window.alert(5 + 6);
< /script >

< /body >
< /html >

Try it out . . .

Action HTML elements

To access an HTML element from JavaScript, you can use the document.getElementById (id) method.

Use the "id" property to identify HTML elements and innerHTML to get or insert element content:

< ! DOCTYPE html >
< Html >
< body >

< h1. My first Web page, slt; /h1 >

< p id s "demo" sgt; my first paragraph slt; /p >

< script >
document.getElementById ("demo"). innerHTML . . . " paragraph modified."
< /script >

< /body >
< /html >

Try it out . . .

The javaScript statement above (in the hashtag) can be executed in a web browser:

Document.getElementById ("demo") is JavaScript code that uses the id property to find HTML elements. This method is defined in HTML DOM.

innerHTML = "Paragraph changed." Is JavaScript code that is used to modify the HTML content (innerHTML) of an element.


In this tutorial

In most cases, in this tutorial, we'll use the methods described above to output:

The following example directly writes the element of id"demo" into the HTML document output:


Write to HTML document

Use the document.write() method to write content to html documents. This feature can be used to write text and HTML.

For testing purposes, you can write JavaScript directly in html documents:

< ! DOCTYPE html >
< Html >
< body >

< h1. My first Web page, slt; /h1 >

< My first paragraph. < /p >

< script >
document.write(Date());
< /script >

< /body >
< /html >

Try it out . . .
JavaScript output

Use document.write() to output writes only to the document.

If document.write is executed after the document is loaded, the entire HTML page is overwritten.

< ! DOCTYPE html >
< Html >
< body >

< h1. My first Web page, slt; /h1 >

< My first paragraph. < /p >

< button onclick s "myFunction()" sgt; dot me slt; /button >

< script >
function myFunction() {
document.write(Date());
}
< /script >

< /body >
< /html >

Try it out . . .


Write to the console

If your browser supports debugging, you can use the console .log() method to display JavaScript values in your browser.

Use F12 in your browser to enable debug mode and click on the "Console" menu in the debug window.

< ! DOCTYPE html >
< Html >
< body >

< h1. My first Web page, slt; /h1 >

< script >
a = 5;
b = 6;
c = a + b;
console.log(c);
< /script >

< /body >
< /html >

Try it out . . .
Tip: The console.log() method lets you see what you're outputing on the page, making it easier for you to debug javascript;

Did you know that?

JavaScript output Debugging in a program is the process of testing, finding, and reducing bugs (errors).