Hello World Program in JavaScript
JavaScript is one of the most popular and primary languages in the web development community. There are multiple ways to print "Hello World Porgram" in JavaScript, and for that, we first must know how to insert JavaScript code in an HTML file using the <script> tag. There are two ways to do that either internally or externally.
If you ask programmers what was their first program? Most of them would say the "Hello World" program. In Javascript, it is very easy to print the javascript hello world program.
In this article, you will begin your journey into programming by exploring different ways to write the "Hello World program" in JavaScript.
Ways To Print Hello World In Javascript
We will use these three ways to print 'hello world program in javascript'
- console.log()
- alert()
- document.write()
1. Using console.log()
The console.log() is a function used to log messages on the web console.
A function in programming is like a black box that takes an input and returns an output by doing some processing.
Let's use console.log to print JavaScript Hello, World!:
In our case, the console.log takes the text we want to display as input and prints it on the screen as the output.
Output: To see the output, open the HTML file with this script in your browser and press F12 or right-click and select inspect then switch to the second tab named console.
You will find the following output:
console.log() is often used in debugging the code (finding and fixing the errors).
2. Using document.write()
document.write() is another function you can use to print Hello World program in JavaScript. Unlike console.log it prints the message on the HTML document instead of the console.
Let's use document.write to print javascript hello world program :
To see the output, open the HTML document in your browser:
There is a better alternative to display text on an HTML document using DOM manipulation which is not difficult but beyond the scope of this article.
3. Using alert()
The last function we will use to print Hello World in JavaScript is alert.
This function displays a pop-up box over the current window with the specified message.
Takeaway:
- You can print Hello World program in JavaScript using console.log(), document.write(), alert() functions.
Conclusion
- Hello World Program in JavaScript is a way to display the “Hello, World” message on the screen.
- Hello world program is first code which is generally written by programmers.
- JavaScript is a programming language used to make web pages interactive.
- You can print Hello World using one of the following functions in JavaScript:
- console.log()
- document.write()
- alert()
- JavaScript hello world program is a fundamental step for any aspiring programmer, marking the beginning of their journey into web development and software engineering.