Tutorials Tonight
Follow Us

3 Ways To JavaScript Hello World

In this tutorial, you will learn to write your first javascript program. We will see how do you write JavaScript programs. You will learn to output 'Hello, World!' using javascript.

  1. Hello World In Javascript
  2. Internal JavaScript
  3. External January
  4. Running javascript without browser

Javascript Hello World

"Hello World" is the first program written by programmers. In javascript, you can write hello world in 3 different ways:

  1. console.log
  2. alert
  3. document.write

1. console log hello world

The first way to write hello world in javascript is to use the console.log function. This function is used to output the text in the console. The syntax is as follows:


// console.log outputs in the console
console.log("Hello World!");

The output will be:

Hello World!

2. alert hello world

The second way to write hello world in javascript is to use the alert method.

The alert method creates an alert box like a pop-up window and displays the text in the alert box.

Note: All the execution of javascript is paused until the alert box is closed.


// alert outputs in a alert box
alert("Hello World!");

3. document.write hello world

The third way to write hello world in javascript is to use the document.write method.

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, it will overwrite the existing content in the HTML document.


// document.write outputs in the HTML document
document.write("Hello World!");
▶ Run the code

We have seen all three ways to write hello world in javascript. Now as a beginner, we will see how to run Javascript files with a browser or without a browser.

Internal javascript

To write javascript code in an HTML file use the <script> tag and place your javascript code inside it.

You can put the <script> tag anywhere in the file but it is recommended to place it just before the end of the <body> tag.


<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <title>Internal javascript</title>
  <h1>Using internal javascript</h1>
  <script>console.log("Hello World!");</script>
▶ Run the code

External Javascript

To write the "Hello World" program in an external javascript file, you have to create a file with a .js extension.

In this file, you have to write javascript code. To output "Hello World" in this file, use the console.log() method.

Here is the code for the hello-world.js file:

hello-world.js file

console.log('Hello, World!');

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.

hello-world.html file

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <title>External javascript</title>
  <h1>Connecting external javascript</h1>
  <script src="./hello-world.js"></script>
▶ Run the code

Running javascript file without browser

You can also run a javascript code without the browser. To do that, you have to use nodeJs.

To run a javascript code without the browser, you have to install nodeJS first. To install nodeJS on your system, follow the instructions on nodejs.org.

Once you have installed nodeJS, you can run a javascript code by using the nodeJS command.

Suppose your hello-world.js file is saved in /js/hello-world.js.

To run this file run the following command:

node ./js/hello-world.js




In this tutorial, you have learned how to write the hello world program in javascript. Also, we learned how to run javascript programs with a browser and without a browser.

Frequently asked questions

  1. How do you say hello world in JavaScript?

    You can use console.log() to display "Hello, World!" in the console and use the document.write() method to write it in the webpage.

  2. How do you display hello world in an alert box using JavaScript?

    Use the alert() method to display "Hello, World!" in the alert box.

  3. how do you write hello world in an alert box?

    To write "Hello World!" in the alert box write alert("Hello World!")