- Using console.log
- Using alert
- Using document.write
Table Of Contents
1. Using console.log
One of the methods of console objects is console.log. This method is used to output the text in the console.
// console.log outputs in the console console.log("Hello World!");
To open console in the browser, you can use the F12 key. You can also use the ctrl + shift + i key combination.
The output will be:
2. Using alert
To use the alert method, you need to add the following code in the script tag of the body tag.
// alert outputs in an alert box alert("Hello World!");
3. Using document.write
The document.write method writes the text in the HTML document. You will see the output in the browser.
Note: It is not recommended to use the document.write method because when it is executed after the page is loaded completely, it will overwrite the existing content in the HTML document.
// document.write outputs in the HTML document document.write("Hello World!");
You can put the
<script> tag anywhere in the file but it is recommended to place it just before the end of the
Here is the code for the
Now you can connect it to some HTML file by using
<script src="hello-world.js"></script> tag.
Now run the page and you will see "Hello, World!" in the console.
Here is the code for the HTML file.
nodeJS first. To install
nodeJS on your system, follow the instructions on nodejs.org.
Once you have installed
hello-world.js file is saved in
To run this file run the following command:
Frequently asked questions
You can use
console.log()to display "Hello, World!" in the console and use the
document.write()method to write it in the webpage.
alert()method to display "Hello, World!" in the alert box.
how do you write hello world in an alert box?
To write "Hello World!" in the alert box write