JavaScript Basic Program
This basic javascript program is used to output a “Hello World!!” message in the browser. Javascript code must be inserted inside <script>
tag within the HTML file.
<html>
<head>
<title>JavaScript Basic Program</title>
</head>
<body>
// Writes text directly into the web page
<script language="javascript" style="text/javascript">
document.write("Hello World!!");
</script>
</body>
</html>
In the next tutorial, we will share how to run these programs in your computer. For now, you can run above HTML code directly via online editor such as http://codepen.io/pen/
Using alert(), console.log(), and document.write()
Here’s a another HTML program that use different JavaScript methods—alert(), console.log(), and document.write() to print some message at different places.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Basics Example</title>
<script>
// Shows a popup alert box when the page loads
alert("Welcome to JavaScript!");
// Prints a message in the browser's console (press F12 to see it)
console.log("This runs from the head.");
</script>
</head>
<body>
<h1>JavaScript Output Example</h1>
<script>
// Writes text directly into the web page
document.write("Hello World!!");
</script>
</body>
</html>
What Happens When You Run This:
- As the page loads, you’ll first see an alert box:
- “Welcome to JavaScript!”
- After dismissing the alert, check your browser console (F12 or right-click > Inspect > Console): You’ll see:
- “This runs from the head.”
- On the web page, you’ll see the text:
- Hello World!!
Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |