Site icon TestingDocs.com

JavaScript Hello World Application

Overview

In this post, we will write a simple JavaScript Hello World application. Every web browser has a JavaScript interpreter that understands how to read JavaScript programs. So, all you need is a web browser like IE, Mozilla Firefox, or Chrome to execute JavaScript.

JavaScript Code

In this function, we use an alert dialog to display a message. For example, as shown below, you might use this alert function to display a Hello World message to the user with an OK button.

The alert dialog displays a message and waits until the user clicks OK.

<script>
/*JavaScript Code*/
 function helloWorld(){
 window.alert("Hello World!");
}
</script>

 

Now let us call this function from a simple HTML webpage.

HTML Code

HTML (HyperText Markup Language) is used to make web pages. A markup language is used to annotate documents so that they’re not just plain text. The markup tells a web browser how to display the text and what to do with it.

 

<html>
<script>
/*JavaScript Code*/
 function helloWorld(){
 window.alert("Hello World!");
}
</script>

<title>Sample WebPage</title>
<body>
<h1>Hello World JavaScript Application</h1>
<p> Click the button to say Hello World </p>
 <form>
 <input type="button" value="Click Here" onclick="helloWorld();"/>
 </form>
 </body>
</html>

 

Save the code with an HTML file extension on your computer. The function will be invoked when you click on the button “Click here.”

JavaScript Hello World Output

Open the webpage in a browser. For example, in the Chrome browser.

 

 

Click on the button “Click here” to execute the JS function.

The page would display a “Hello World” message with an OK button. That’s it. We have successfully created a simple JavaScript HelloWorld application.

JavaScript Tutorials

JavaScript tutorials on this website can be found at:
https://www.testingdocs.com/javascript-tutorial/

To learn more about JavaScript, visit

https://www.javascript.com/

Exit mobile version