Document.Write() Method in JavaScript

JavaScriptIn JavaScript, Document.Write() method is used to write a string to the HTML document.  It takes a string as a parameter.  That string could be a plain text or a text in the form of HTML code.  Sample code for both scenarios is given below.

 

 

JavaScript Text:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Tutorial </title>
</head>
<body>
<script type="text/javascript">
var message = "Hello World";
document.write(message);
</script>
</body>
</html>
<!DOCTYPE html> <html> <head> <title>JavaScript Tutorial </title> </head> <body> <script type="text/javascript"> var message = "Hello World"; document.write(message); </script> </body> </html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Tutorial </title>
</head>
<body>
<script type="text/javascript">
var message = "Hello World";
 
document.write(message);
 
</script>
 
</body>
</html>

HTML Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Tutorial </title>
</head>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>
<!DOCTYPE html> <html> <head> <title>JavaScript Tutorial </title> </head> <body> <script type="text/javascript"> document.write("<h1>Hello World!</h1>"); </script> </body> </html>
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Tutorial </title>
</head>
<body>
<script type="text/javascript">
 
document.write("<h1>Hello World!</h1>");
 
</script>
 
</body>
</html>