๐Ÿ  JavaScript Basics
Estimated reading: 4 minutes 12 views

๐Ÿ‘‹ JavaScript Hello World โ€“ Your First JS Program Explained

๐Ÿ’ก Starting with your first line of JavaScript code is an exciting step toward mastering web development.

The “Hello, World!” program is the traditional starting point for learning any new programming language. Itโ€™s simple, but powerfulโ€”it confirms that your environment is set up correctly and that your code can run in the browser.


๐Ÿง  What Youโ€™ll Learn

โœ… How to write your first JavaScript program
โœ… How to use the <script> tag
โœ… How to display output using alert() and console.log()
โœ… Where to place JavaScript code in an HTML file


๐Ÿ“ Writing Your First JavaScript Code

โœ… Method 1: Inline JavaScript with <script> Tag

<!DOCTYPE html>
<html>
<head>
<title>Hello World in JS</title>
</head>
<body>
<script>
alert("Hello, World!");
</script>
</body>
</html>

๐Ÿงพ Explanation

  • <!DOCTYPE html> โ€” Declares the HTML5 document type.
  • <html> โ€” Root element of the HTML page.
  • <head> โ€” Contains metadata like the title.
  • <body> โ€” Contains visible page content.
  • <script> โ€” This is where you write JavaScript.
  • alert("Hello, World!"); โ€” Shows a popup alert with the message.

โš ๏ธ Note: This method uses alert(), which interrupts the user’s experience. It’s best used for testing or debugging.


๐Ÿ–ฅ๏ธ Method 2: Using console.log() for Developers

<!DOCTYPE html>
<html>
<head>
<title>Console Example</title>
</head>
<body>
<script>
console.log("Hello, World!");
</script>
</body>
</html>

๐Ÿงพ Explanation

  • console.log("Hello, World!"); โ€” Prints the message in the browserโ€™s Developer Console.
  • Open your browserโ€™s dev tools (usually F12 or Ctrl + Shift + I) and go to the Console tab to see the output.

โœ… This is the preferred method for debugging and testing JavaScript as a developer.


๐Ÿ“‚ Method 3: External JavaScript File

Separate your JS code from HTML by using an external .js file.

๐Ÿ“„ index.html

<!DOCTYPE html>
<html>
<head>
<title>Hello from External JS</title>
</head>
<body>
<script src="hello.js"></script>
</body>
</html>

๐Ÿ“„ hello.js

console.log("Hello, World from external file!");

๐Ÿงพ Explanation

  • src="hello.js" โ€” Loads JavaScript from an external file.
  • Keeps your HTML clean and maintains a separation of concerns.

๐Ÿ’ก Best Practices

โœ… Prefer console.log() for debugging, not alert()
โœ… Use external JavaScript files for large codebases
โœ… Place <script> tags at the bottom of <body> or use defer to improve page load performance


๐Ÿ› ๏ธ Where Should You Write JavaScript?

PlacementDescriptionBest Use Case
Inside <head>Executes before HTML is parsedUse with defer or async
End of <body>Executes after the HTML is fully parsedMost common placement
External .js fileKeeps code modular and reusableRecommended for all projects

๐Ÿ“Œ Summary

Youโ€™ve just written your first “Hello, World!” program in JavaScript!
This small step is a big milestoneโ€”from here, you’ll learn how to:

  • Handle user interactions
  • Modify the DOM
  • Work with variables, functions, and loops
  • Build real applications!

โ“ FAQ โ€“ JavaScript Hello World

โ“ What is the purpose of a “Hello World” program?

A: It verifies that JavaScript is correctly running in your environment.

โ“ Should I use alert() or console.log()?

A: Use console.log() for development and debugging; alert() is intrusive and rarely used in production.

โ“ Can I run JavaScript without an HTML file?

A: Yes, you can run JavaScript in a browser console or using Node.js on your computer.

โ“ What does console.log() do in JavaScript?

A: It prints output to the browser’s Developer Console.

โ“ How do I include JavaScript in HTML?

A: Use the <script> tagโ€”either inline or with a src attribute to link an external .js file.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

JavaScript โ€” Hello World

Or Copy Link

CONTENTS
Scroll to Top