JavaScript Basic Program
Let us write a complete Hello World program in JavaScript and understand each line.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World</title>
</head>
<body>
<h1>My first JavaScript page</h1>
<script>
console.log("Hello, World!");
alert("Hello, World!");
</script>
</body>
</html>
How it works
- The
<script>tag tells the browser that the code inside is JavaScript. console.log()prints a message in the browser console (open it with F12).alert()shows a pop-up box with the message. The page waits until you click OK.- Each statement ends with a semicolon
;.
The console shows:
Output of the above program is
Hello, World!
The same program in an external file
<script src="app.js"></script>
// app.js
console.log("Hello, World!");
Note:
alert() is fine to learn, but real websites avoid it because it blocks the visitor. Use console.log() for testing and the DOM for showing messages on the page.
Summary
- JavaScript runs inside a
<script>tag or in a linked file. console.log()andalert()show a message.- Next: JavaScript Output.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |