Whenever we start to learn any language we prefer to write or print “Hello World” in the beginning. So, here we also going to see how can we print “Hello World” using JavaScript. To print “Hello World” on the screen we need to use HTML also, and to do that we have to use a script tag in the HTML document.
Script tag enables the use of JavaScript in the HTML file and it can be inside of the head tag or body tag depending on when we want to load our JavaScript file onto the browser.
Example: In this example, we will not include any JavaScript code, here we are just putting the script tag in the HTML code to show you, how to use internal & external JavaScript code.
HTML
<!DOCTYPE HTML>
< html >
< head >
< title >JavaScript Course - Hello World</ title >
< script src = "main.js" >
// External JavaScript Code
</ script >
</ head >
< body >
< p >Happy Learning of JavaScript</ p >
< script >
// Internal JavaScript Code
</ script >
</ body >
</ html >
|
The most common methods of printing “Hello World” are:
Example: In this example, we will print the legendary “Hello World” in the window console.
Javascript
console.log( 'Hello World' );
|
Output: Press Ctrl+Shift+J
to see the output in the browser console.
Example: In this example, we will print the “Hello World” in the HTML document.
Javascript
document.write( 'Hello World' );
|
Output:
Example: In this example, we will print the “Hello World” on the browser window with some message or warning.
Output:
Each of the above methods has different ways of outputting the content. Though ‘document.write()’ is used when we want to print the content onto the document which is the HTML Document. Also ‘console.log()’ is mainly used when we are debugging JavaScript code and the ‘alert()’ is used to show an alert box on the browser window with some message or warning.
Last Updated :
08 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...