Home » JavaScript

How to write an HTML output in JavaScript?

Here, we are going to learn how to write a value on HTML i.e. as an HTML output in JavaScript?
Submitted by IncludeHelp, on February 02, 2019

When we are writing a JavaScript code and want to write on the HTML, we can use document.write() function, it accepts a simple string, variable, concatenated string, combinations of the multiple variables and print on the HTML document.

document.write()

Here, write() is an HTML DOM method and it writes the string/text to the HTML directly.

Syntax:

    document.write(string);
    or
    document.write(expression1, expression2, expression3, ...);

Example:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
	<script>		
		document.write("Hello world");
		document.write("<br>"); //HTML tag <br> for a new line
		document.write(10);
		document.write("<br>");
		document.write(10+20);
		document.write("<br>");		
		document.write("Hello", 10, "World");
		document.write("<br>");		
		document.write("Hello" + 10 + "World");
		document.write("<br>");		
		//writing including HTML tags
		document.write("Hello<br>world");
	</script>
</body>
</html>

Output

Hello world
10
30
Hello10World
Hello10World
Hello
world





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.