At the start of learning any computer language we tried to print the “Hello World” in that language, here we will do the same and explain the whole code as well.
Whenever we write javascript we make use of the ‘script’ tag. We know what a normal HTML document is made up of The ‘script’ tag is used to write javascript code and this ‘script‘ tag is either placed inside the ‘head‘ tag or inside the ‘body‘ tag.
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, that 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 >
|
We can put the script tag inside the ‘head’ or ‘body’ tag. Though it should be noted that each choice of putting the ‘script’ tag has its own consequences. For now, you can put the script tag anywhere you want.
Printing Hello World In order to print the famous ‘Hello World’ sentence to the screen, we can make use of different methods that javascript provides. The most common are:
-
Example: Using console.log() method In this example, we will print the legendary “Hello World” in the window console.
Javascript
<script>
console.log( 'Hello World' );
</script>
|
Output: Hit Ctrl+Shift+J
to see the output in the browser console.

-
Example: Using document.write() method, in this example, we will print the “Hello World” in the HTML document.
Javascript
<script>
document.write( 'Hello World' );
</script>
|
Output:
-
Example: Using alert() method, in this example, we will print the “Hello World” on the browser window with some message or warning.
Javascript
<script>
alert( 'Hello World' );
</script>
|
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Mar, 2023
Like Article
Save Article