To understand this example, you should have the knowledge of the following JavaScript programming topics:
A "Hello, World!" is a simple program that prints Hello, World! on the screen. Since it's a very simple program, this program is often used to introduce a new programming language to beginners.
We will use these three ways to print 'Hello, World!'.
- console.log()
- alert()
- document.write()
1. Using console.log()
console.log() is used in debugging the code.
Source Code
// the hello world program
console.log('Hello World');
Output
Hello, World!
Here, the first line is a comment.
// the hello world program
The second line
console.log('Hello, World!');
prints the 'Hello, World!' string to the console.
2. Using alert()
The alert() method displays an alert box over the current window with the specified message.
Source Code
// the hello world program
alert("Hello, World!");
3. Using document.write()
document.write() is used when you want to print the content to the HTML document.
Source Code
// the hello world program
document.write('Hello, World!');