JavaScript Hello World
Last Updated :
21 Aug, 2024
JavaScript Hello World refers to the basic and traditional first program when learning JavaScript. It involves displaying the phrase “Hello World” using simple methods like console.log(), document.write(), or alert(), introducing beginners to fundamental JavaScript syntax and output techniques.
Using console.log() method
The console.log() method prints the message to the browser console. This approach is mainly used for debugging purposes and checking outputs while developing code.
Example: In this example, we will print the legendary “Hello World” in the window console.
JavaScript
// Using console.log
console.log('Hello World');
Using document.write() Method
Using the document.write() method in JavaScript allows you to display “Hello World” directly on the webpage. This method is straightforward and places the text within the HTML document. However, itâs generally only suitable for simple demos or initial page load.
Example: In this example, we will print the “Hello World” in the HTML document.
JavaScript
// Using document.write
document.write('Hello World');
Output:
Hello world
Using alert() Method
The alert() method displays a popup alert box with the message. Itâs useful for simple notifications or warnings but should be used sparingly as it disrupts user interaction.
Example: In this example, we will print the “Hello World” on the browser window with some message or warning.
JavaScript
// Using alert
alert('Hello World');
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.
Please Login to comment...