Tutorials Tonight
Follow Us

JAVASCRIPT HELLO WORLD


In this tutorial, you will learn to write your first javascript program. You will learn to output 'Hello, World!' using javascript.

  1. Hello World In Javascript
  2. Running javascript in nodeJS
  3. Hello world in webpage

Hello World In Javascript

To write the "Hello World" program in javascript we use the console.log() method.

console.log() method is used to output something in console or in terminal (when using nodeJS).

But the question is where do we write our javascript code?

You can write javascript code in any HTML file within the <script> tag (internal javascript) or in an external javascript file (a file saved with .js extension).

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>console.log("Hello World!");</script>
</body>
</html>
▶ Run the code

External Javascript File

To write "Hello World" program in external javascript file, you have to create a file with .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 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

Hello World in the webpage using javascript

You can directly display "Hello World" on the webpage using javascript. To do that use document.write() method.

The document.write() method writes the string to the webpage. You can use it to write any string on the webpage.

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

Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hello world using javascript</title>
</head>
<body>
  <h1>Hello world using javascript</h1>

  <script>
    document.write("Hello, World!");
  </script>
</body>
</html>
▶ Run the code

You can also alert a message to the user using alert() method.

Example

alert('Hello, World!');

Conclusion

In this tutorial, you have learned how to write a javascript program in an HTML file and how to run it without the browser.

Frequently asked questions

Here are some frequently asked questions about this tutorial.

  • Q: 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.

  • Q: How do you display hello world in an alert box using JavaScript?

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



Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.