When you start Learning any programming First need to write the basic syntax of it. So Let’s Start With a JavaScript Hello World first program.
JavaScript is a very powerful and popular language nowadays. It mainly used in a web application with HTML and CSS. And it’s also used for server-side programming using nodejs. In this tutorial, you will learn How to write a Hello World on an HTML page using a JavaScript Function.
When using a browser, you can use a print out function and results called “console.log”.
How to start First Javascript Program
Before start JavaScript, you must know basic of HTML. Follow below this tutorial link:-
After that, you need to use a <head> tag. Inside head tag, you have to use a <script> tag. It’s not always required a <script> tag inside <head> tag.
Let’s see Javascript Hello World Examples
Here is 3 ways to print a “Hello world” in JavaScript programming language.
- Print in HTML Tag
- Use Alert box
- Pring in Console
1. Print Javascript Hello world Function in HTML Tag
It’s a very easy, use a <p> tag with an id. Then write code in <script>. See below example of the Javascript print hello world.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</body>
</html>
Output: A launched in browser, you can see the output.
2. JavaScript Alert Hello World | Alert box
This is also easy just to use an alert() function with a message. An alert box used for giving acknowledgment to the user.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p id="demo"></p>
<script>
alert("Hello, World!");
</script>
</body>
</html>
Output: A pop will come on the browser window with a message and close button.
3. JavaScript Hello World Console
Using the console.log()
method, also print a string. This is important when you required to see the application log at the development phase.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<p id="demo"></p>
<script>
console.log("Hello, World!");
</script>
</body>
</html>
Output: You have to open inception element -> console, to see the output in the browser.
Hope you learn how to write the first JavaScript. Do comment if you have any doubt or suggestions.
Note: The JavaScript Hello World Examples are tested on Safari browser (Version 12.0.2).
OS: macOS 10.14 Mojave
Version: HECMAScript 2018