Your First JavaScript Program – Hello World Explained
Quick Answer: Your first JavaScript program can print a simple message using console.log().
Write console.log("Hello, World!"); in your browser console or a JavaScript file. Run it to display Hello, World!.
You will also learn how JavaScript runs inside a browser. Three visual animations explain the complete process.
01 Why Write a Hello World Program?
Imagine you are creating a website for a grocery shop.
The shop owner wants to welcome customers when they visit.
Before building the complete website, you need to check JavaScript.
You can start by printing a simple welcome message.
This small program helps you understand three important things:
- Where to write JavaScript code.
- How to run JavaScript in a browser.
- How to check the result of your program.
Hello World is a common starting program for beginners.
It helps you test your development environment before writing larger programs.
Your First JavaScript Statement
Example 1: Print Hello World
console.log("Hello, World!");
Expected output
Hello, World!
Remember: JavaScript is case-sensitive. Write console.log, not Console.Log.
02 How Does Your First JavaScript Program Work?
JavaScript is a programming language used to make websites interactive.
It can update content, handle user actions, and process information.
For now, we will use JavaScript to display a message.
Animation 1: JavaScript Execution Architecture
This diagram shows how JavaScript moves from code to output.
Figure 1: A continuously animated, three-stage JavaScript execution workflow. Each stage remains readable throughout.
Understanding Each Stage
- Source code: You write instructions using JavaScript syntax.
- JavaScript engine: The engine runs your JavaScript instructions.
- Console: The console displays the message printed by your program.
JavaScript engine = software that runs JavaScript code.
Console = a developer tool that displays messages and errors.
The browser contains a JavaScript engine and developer tools.
You can use both without installing a separate JavaScript runtime.
03 Run Hello World in Your Browser
The browser console is the easiest place to start.
You do not need to create a project for this example.
Method 1: Use Browser Developer Tools
- Open Chrome, Edge, or Firefox.
- Open a normal webpage.
- Press F12 to open Developer Tools.
- Select the Console tab.
- Type the JavaScript statement shown below.
- Press Enter to run it.
Example 2: Print a Grocery Shop Welcome Message
console.log("Welcome to Fresh Mart!");
Expected output
Welcome to Fresh Mart!
Browser tip: Some browsers show a warning before allowing pasted console code. Type the example yourself when learning.
Animation 2: Live JavaScript Console Simulator
Watch the browser execute a JavaScript statement.
The animation repeats automatically. It uses clear text and fixed layouts.
JavaScript prints Hello, World! in the console.
Figure 2: An automatically repeating JavaScript execution demonstration. The output uses direct text updates, without scaling or blur effects.
04 Understand console.log() Line by Line
Now examine the statement you just executed.
Example 3: Basic JavaScript Syntax
console.log("Hello, World!");
| Part | Meaning |
|---|---|
console |
A built-in object used for developer messages. |
. |
Accesses a property or method on an object. |
log |
A method that writes information to the console. |
() |
Parentheses hold the values passed to a method. |
"Hello, World!" |
A string containing the message to display. |
; |
Marks the end of the statement explicitly. |
Object = a value that can contain data and methods.
Method = a function available through an object.
String = text stored inside quotation marks.
Statement = an instruction that JavaScript can execute.
Do We Need a Semicolon?
JavaScript can insert missing semicolons in many situations.
However, automatic insertion does not work safely in every case.
Using semicolons consistently can make beginner examples easier to follow.
Example 4: Two Valid JavaScript Statements
console.log("Welcome!");
console.log("Fresh Mart is open.");
Expected output
Welcome! Fresh Mart is open.
05 Run JavaScript Using an HTML File
Real websites often use JavaScript together with HTML.
HTML defines the structure of a webpage.
JavaScript can update that structure after the page loads.
Method 2: Create Your First HTML Page
- Open Visual Studio Code or another text editor.
- Create a file named
index.html. - Copy the complete example below.
- Save the file.
- Open the file in your browser.
Example 5: JavaScript Inside HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fresh Mart</title>
</head>
<body>
<h1>Fresh Mart</h1>
<script>
console.log("Welcome to Fresh Mart!");
</script>
</body>
</html>
What appears on the webpage?
The browser displays the heading Fresh Mart.
What appears in the console?
Welcome to Fresh Mart!
The <script> element allows JavaScript inside an HTML document.
Here, the browser executes the script while processing the page.
Method 3: Use a Separate JavaScript File
Keeping JavaScript separate makes larger projects easier to manage.
Create two files in the same folder.
Example 6A: index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fresh Mart</title>
<script src="app.js" defer></script>
</head>
<body>
<h1>Fresh Mart</h1>
</body>
</html>
Example 6B: app.js
console.log("Welcome to Fresh Mart!");
Expected console output
Welcome to Fresh Mart!
The src attribute tells the browser where the JavaScript file is located.
The defer attribute delays script execution until HTML parsing finishes.
Good practice: Use separate JavaScript files as your application grows. They help you organize code without mixing everything into one HTML file.
06 Display Hello World on the Webpage
The console is useful for developers.
However, customers need to see messages directly on the webpage.
JavaScript can update HTML content using the DOM.
DOM = the browser's object representation of an HTML document.
Example: Show a Grocery Shop Greeting
Example 7: Update HTML Content with JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fresh Mart Greeting</title>
</head>
<body>
<h1>Fresh Mart</h1>
<p id="welcome"></p>
<script>
const message = "Welcome to Fresh Mart!";
document.getElementById("welcome").textContent = message;
</script>
</body>
</html>
Expected webpage content
Fresh Mart Welcome to Fresh Mart!
Animation 3: From JavaScript to Visible Webpage Content
This animation explains how JavaScript changes an HTML element.
const message = "Welcome to Fresh Mart!"; element.textContent = message;JavaScript updates the text inside the selected HTML element.
Figure 3: JavaScript updates a grocery shop webpage. The animation highlights the result without moving or blurring the text.
Understand the Important Parts
Represents the webpage loaded in the browser.
Finds an HTML element using its ID.
Reads or changes the text inside an element.
Declares a variable that cannot be reassigned.
Important: getElementById() returns null when the element does not exist. Accessing textContent on null causes an error.
07 Understand console.log(), alert(), and textContent
JavaScript offers different ways to show information.
Each option serves a different purpose.
| Method | Where Output Appears | Common Use |
|---|---|---|
console.log() |
Developer console | Debugging and checking values |
alert() |
Browser dialog | Simple demonstrations and urgent messages |
textContent |
HTML element | Updating visible webpage text |
Example: Display an Alert
Example 8: Browser Alert
alert("Welcome to Fresh Mart!");
Expected result: The browser opens a dialog showing the welcome message.
Use alerts carefully because they interrupt normal page interaction.
For most website messages, updating HTML content is more suitable.
08 Common JavaScript Beginner Mistakes
Mistake 1: Using Incorrect Capital Letters
Incorrect example
Console.Log("Hello, World!");
JavaScript treats uppercase and lowercase letters as different characters.
Correct example
console.log("Hello, World!");
Mistake 2: Forgetting Quotation Marks
Incorrect example
console.log(Hello World);
This causes a syntax error because the message is not valid JavaScript.
Correct example
console.log("Hello World");
Mistake 3: Forgetting Parentheses
Incorrect example
console.log "Hello World";
Correct example
console.log("Hello World");
Mistake 4: Looking for Console Output on the Page
console.log() does not directly display text inside HTML.
Open Developer Tools and select the Console tab.
Use textContent when customers must see the message.
Mistake 5: Running Code Before an HTML Element Exists
JavaScript cannot update an element before the browser creates it.
Place the script after the element or use defer.
09 JavaScript Hello World Interview Questions
1. What is JavaScript?
Answer: JavaScript is a programming language. We use it to make websites interactive. It can also run outside browsers using suitable runtimes.
2. What does console.log() do?
Answer: It writes information to the console. Developers use it to inspect values and understand program behavior.
3. Does JavaScript need a compiler?
Answer: JavaScript runs through a JavaScript engine. Modern engines can interpret and compile code internally. You do not need a separate compilation step for this example.
4. What is the difference between HTML and JavaScript?
Answer: HTML defines webpage structure. JavaScript adds behavior and can change webpage content.
5. Can JavaScript run without a browser?
Answer: Yes. JavaScript can run through environments such as Node.js. These environments provide their own runtime features.
6. What is the difference between console.log() and document.write()?
Answer: console.log() writes to the console. document.write() writes into an HTML document. Avoid document.write() for normal page updates because it can replace existing content.
10 Hands-On JavaScript Practice
Try these exercises without copying the answers first.
Exercise 1: Print Your Shop Name
Write a JavaScript statement that prints Fresh Mart.
Solution
console.log("Fresh Mart");
Expected output
Fresh Mart
Exercise 2: Print Three Grocery Items
Print three grocery items using separate statements.
Solution
console.log("Rice");
console.log("Milk");
console.log("Sugar");
Expected output
Rice Milk Sugar
Exercise 3: Print a Price
Print the price of a grocery item.
Solution
console.log("Rice price:", 60);
Expected output
Rice price: 60
Here, the number 60 is not written inside quotation marks.
JavaScript can work with numbers as well as text.
Exercise 4: Print a Simple Calculation
Calculate the total price of two grocery items.
Solution
console.log(60 + 40);
Expected output
100
JavaScript calculates the expression before printing the result.
Exercise 5: Show a Greeting on the Webpage
Create a paragraph with the ID message.
Use JavaScript to display Thank you for visiting Fresh Mart!.
Solution
<p id="message"></p>
<script>
document.getElementById("message").textContent =
"Thank you for visiting Fresh Mart!";
</script>
Expected webpage text
Thank you for visiting Fresh Mart!
11 Frequently Asked Questions
Is JavaScript difficult for beginners?
JavaScript has a simple starting point. You can run small programs directly in your browser. More advanced concepts take practice.
Do I need Visual Studio Code?
No. You can start using your browser console. A code editor becomes useful when creating projects.
Can I write JavaScript without HTML?
Yes. You can run JavaScript directly in the browser console. You can also use a JavaScript runtime outside the browser.
Why is Hello World important?
It confirms that your code can run successfully. It also introduces basic syntax and output.
Should I use console.log() in production applications?
Use it carefully. Avoid printing passwords, tokens, or sensitive customer information. Production applications usually need structured logging and suitable monitoring.
12 What Should You Learn Next?
You have now written and executed your first JavaScript program.
You also learned how JavaScript works with HTML.
You can now:
- Write basic JavaScript statements.
- Print messages using
console.log(). - Run JavaScript through browser Developer Tools.
- Connect a JavaScript file to an HTML page.
- Update visible webpage text using JavaScript.
- Understand the basic JavaScript execution workflow.
Key takeaway: Start with one JavaScript statement. Run it, check its output, and understand each part. These small steps prepare you for variables, data types, operators, functions, and DOM programming.
Continue Your JavaScript Learning Journey
Your next learning goal is to understand how JavaScript stores information.
Practice using variables, numbers, strings, and simple calculations.
Then use those values to build small, useful website features.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.