Home : Internet : JavaScript : Write

JavaScript "document.write"

One of the most basic JavaScript commands is document.write. This simply prints the specified text to the page. To print text literally, enter the text in single quote marks inside parentheses like so:

	document.write('Hello World!');

The code above will cause the phrase "Hello World!" to appear on the page.

You can also use document.write to print variables. Enter the variable name without quotes, like so:

	var mytext = "Hello again";
	document.write(mytext);

Note that if quote marks are placed around the variable name, the variable name itself will be printed (instead of the variable value). You can also combine variable values and text strings by using the + sign:

	var colour1 = "purple";
	var colour2 = "pink";
	document.write('<p>colour1: ' + colour1 + '<br>colour2: ' + colour2 + '</p>');

Notice the way the variable names are used literally as well as for passing the values. This will print the following:

Remember, text inside quotes will be printed literally, text with no quotes is assumed to be a variable.