document.write() : Output to browser

document.write('Hello World');
Hello World

The above command will print Hello World in the web browser. Full code is here
document.write('Hello World');
We can use document object to send output to the client browser from our JavaScript code block. This is a simple way to send output and we can print any text we want to write. We can print the output within our JavaScript code block only.

We can print variables like this.
my_var='Hello World'
We can print two variables like this
my_var="Hello World"
my_var1="Hello World"

document.write(my_var + my_var1)

Adding Line Break

We can add line break before this by using document.writeln. We can also add a line break like above example where ever required.
document.write(my_var + "<br>"+my_var1 )

Using document.writeln()

document.writeln() will add one line break after the statement. However while displaying, HTML will remove all white space and keep one single space, so new line will not be displayed. To get line break we will keep our code inside <pre> tag.
document.writeln('Hello World');
document.writeln(' Welcome');
Hello World
Exercise Keep the document.write() inside a function. Trigger the function on button click to show the message Hello World

document Object Cookies
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 All rights reserved worldwide Privacy Policy Disclaimer