Tutorials Tonight
Follow Us

JavaScript Hello World In 3 Different Ways


In this tutorial, you will learn to write hello world in Javascript in 3 different ways. JavaScript hello world is the first program written by programmers so we have explained each step of the process.

    Table Of Contents

  1. Hello World In Javascript
    1. Using console.log
    2. Using alert
    3. Using document.write
  2. Internal JavaScript
  3. External JavaScript
  4. Running javascript without browser

Javascript Hello World

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

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

1. Using console.log

The first way to write Hello World in Javascript is to use the console.log method. console is an object that is used to write messages to the console.

One of the methods of console objects is console.log. This method is used to output the text in the console.

Example

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

To open console in the browser, you can use the F12 key. You can also use the ctrl + shift + i key combination.

hello world using console

The output will be:

Hello World!

2. Using alert

The second way to write Hello World in Javascript is by using the alert method. The alert is a method that creates an alert box or a pop-up window with the text you provide.

To use the alert method, you need to add the following code in the script tag of the body tag.

Note: All the execution of javascript code will stop after the alert method is called until the user clicks on the OK button.

Example

// alert outputs in an alert box
alert("Hello World!");
▶ Try It

Output

hello world using alert

3. Using document.write

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

Example

// 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.

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Internal javascript</title>
</head>
<body>
  <h1>Using internal javascript</h1>
  
  <!-- script goes here -->
  <script>
    console.log("Hello World!");
  </script>
</body>
</html>
▶ 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">
<head>
  <meta charset="UTF-8">
  <title>External javascript</title>
</head>
<body>
  <h1>Connecting external javascript</h1>
  
  <script src="./hello-world.js"></script>
</body>
</html>
▶ 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

Output:

output

Conclusion

In this tutorial, you have learned 3 ways for javascript hello world. 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!")