Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
JavaScript Hello World Program
3.
How to Print Hello World in JavaScript Using function??
3.1.
Using Document.write()
3.1.1.
Code
3.1.2.
Output
3.2.
Using Console.log()
3.2.1.
Code
3.2.2.
Output
3.3.
Using Alert()
3.3.1.
Code
3.3.2.
Output
4.
Frequently Asked Questions
4.1.
What is the Hello World function?
4.2.
What is the syntax to print Hello World?
4.3.
What is JavaScript with example?
4.4.
What type of language is JavaScript?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Hello World Program in JavaScript

Introduction

Every web developer in the tech world knows about Javascript. And why would they not? It is a famous language to create the front-end portion of any dynamic webpage. Its users love it for its lightweight nature and huge demand resulting in high pay in the job market. This blog will instruct you on how to write a hello world program in JavaScript. A JavaScript hello world program is the most commonly used method to introduce a language to a newbie. 

javascript hello world

JavaScript Hello World Program

We will soon head to print a JavaScript program to print Hello World on the output screen. But before that, let's get a bit into the theory of what we are about to implement. 

Any text editor can embed your JavaScript hello world code into an HTML page. The JavaScript code is embedded into an HTML page through a <script> tag. We place the <script> tag either in the head tag or body tag. 

JavaScript is a client-side scripting language. So, it provides the user with three ways to print the sentences (output).

  • document.write()
  • console.log()
  • alert()
     

Each of these methods prints the JavaScript hello world output in different ways. The ‘document.write()’ is used to print the strings on the webpage. ‘console.log()’ is mainly used while debugging, and the ‘alert()’ is used when we want the output in a dialog box popping up on the screen. So, you can actually print your JavaScript hello world code in three ways. Interesting right?

How to Print Hello World in JavaScript Using function??

Now, let's finally get to the core and print a JavaScript Hello World code!

Using Document.write()

It is used to print the strings on the web browser.

Code

<!DOCTYPE HTML>
<html>
<body>
  <script>
    document.write("Hello World!")
  </script>
</body>
</html>

Output

document.write() output


You can practice by yourself with the help of Online Javascript Compiler for better understanding.

Using Console.log()

This method is used to write the code while debugging. It writes the JavaScript hello world program to the browser console.

Code

<!DOCTYPE HTML>
<html>
<body>
  <script>
    console.log('Hello World');
  </script>
</body>
</html>

Output

console.log() output

Using Alert()

We use this approach when we want to print our JavaScript hello world output as a pop-up on the screen.

Code

<!DOCTYPE HTML>
<html>
<body>
  <script>
    alert('Hello World');
  </script>
</body>
</html>

Output

alert() output


Must Read Fibonacci Series in JavaScript

Frequently Asked Questions

What is the Hello World function?

In JavaScript, you can define a custom function that prints the “Hello World” string to the console of your browser. You can define similar functions for more control over console logging and debugging.

What is the syntax to print Hello World?

In JavaScript, the most common way for printing anything to the browser console is the “console.log()” function. This function accepts a string and prints it to the console. There are other functions, like console.dir() and console. debug().

What is JavaScript with example?

JavaScript is a dynamically typed scripting language that is commonly used in building frontend and backend web applications. It can also be used in data analytics and machine learning. It uses Just-in-Time compilation for fast execution times.

What type of language is JavaScript?

JavaScript is an easy-to-learn programming language having first-class functions. It is lightweight and may be interpreted or just compiled in time.

Conclusion

In this article, you learned how to implement a simple but famous hello world program in JavaScript. Though the learning might have just begun for you, it should never stop. So please have a look at these related articles for more information.

Do check out The Interview guide for Product Based Companies as well as some of the Popular interview problems from top tech companies like Amazon, Adobe, Google, Uber, Microsoft, etc.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; look at the Top 150 Interview Puzzles interview experiences, and interview bundle for placement preparations.

Happy Learning!

Live masterclass