Your First JavaScript Program
Your First JavaScript Program
console.log sends a value to the host's console. Quoted text is a string, and parentheses supply that string as the function's argument. This is an instruction, not a visible HTML heading.
Before this topic, review Set Up and Run JavaScript if its terms are unfamiliar.
What you’ll learn
- Explain your first javascript program through the school example.
- Trace the shown operations and distinguish their result from displayed output.
- Make the practice change and explain its consequence: The new string is passed to the same output operation.
Work through the school example
This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.
JavaScript · school.js · complete example
console.log("Hello, school!");
Expected output
Hello, school!Run in JavaScript compiler →Introduction
Every programming journey begins with a humble start — the "Hello, World!" program. In JavaScript, this tiny snippet introduces you to the syntax, output methods, and the environments in which JavaScript runs. Whether you're using the browser console, Node.js, or Visual Studio Code, this guide walks you through it all.
1. JavaScript Hello World in the Browser Console
This is the fastest and most accessible way to run JavaScript without any installation.
Step-by-Step:
- Open Google Chrome or any modern browser.
- Right-click anywhere on the page and choose Inspect.
- Go to the Console tab.
- Type the following code and press
Enter:
console.log("Hello, World!");
Output:
Hello, World!
Explanation: console.log() is a JavaScript function used to print messages to the console — great for debugging or quick testing.
2. Hello World using Node.js
Node.js is a JavaScript runtime that allows you to run JS outside the browser, typically for backend development.
Step-by-Step:
- Install Node.js from the official website: https://nodejs.org.
- Open your terminal or command prompt.
- Create a new file named
hello.jsusing a code editor ortouch hello.json terminal. - Add the following code:
// hello.js
console.log("Hello, World from Node.js!");
To Run:
node hello.js
Output:
Hello, World from Node.js!
Explanation: When you run node hello.js, Node.js executes the script file and outputs the result to your terminal.
3. Hello World using Visual Studio Code (VS Code)
VS Code is a lightweight, powerful code editor perfect for writing and testing JavaScript projects.
Step-by-Step:
- Download and install VS Code.
- Open VS Code and create a new folder (e.g.,
HelloWorldProject). - Create a file inside it named
hello.js. - Write the following code:
console.log("Hello from Visual Studio Code!");
Hello from Visual Studio Code!
Run the Script:
- Open the terminal in VS Code (use
Ctrl + `). - Run the script using:
node hello.js
Tip: If you see an error like 'node' is not recognized, make sure Node.js is installed and its path is added to the environment variables.
Advanced Hello World Examples
1. Using Alert in a Browser
This example pops up a dialog box in the browser.
alert("Hello, World!");
Note: This only works in a browser context — not in Node.js.
2. Writing to the HTML Document
This method injects content into the webpage itself.
document.write("Hello, World on the Web Page!");
To use this, include it in a <script> tag inside an HTML file.
<!DOCTYPE html>
<html>
<head><title>JS Hello World</title></head>
<body>
<script>
document.write("Hello, World on the Web Page!");
</script>
</body>
</html>
Key Concepts You Just Learned
console.log(): Used to print messages in browser/Node.js console.alert(): Used in browsers to show popup messages.document.write(): Outputs text directly to the web page.- Execution Environments: Browser Console, Node.js, and VS Code.
Common Questions
Q: Can I run JavaScript without a browser?
Yes, by using Node.js or an online editor like Repl.it or JSFiddle.
Q: Is Node.js necessary to learn JavaScript?
No, but it's useful if you want to run JS outside the browser or build server-side applications.
Q: Why doesn't alert() work in Node.js?
Because alert() is part of the browser's window object and Node.js doesn't have a DOM.
Conclusion
Your first step into JavaScript is complete. You've learned not only how to write a basic Hello, World! but also how to execute it in different environments. From here, you can explore variables, functions, and beyond. The world of JS awaits — happy coding!
Recognize the important boundary
Console formatting can differ by environment. The expected lines here show the message content without developer-tool decorations.
Try a small variation
JavaScript · original source for practice
console.log("Hello, school!");
Your task: Replace Hello, school! with Welcome, Diya!. Predict the result before running the edited program.
The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.
Worked answer and reasoning
The new string is passed to the same output operation.
JavaScript · complete worked variation
console.log("Welcome, Diya!");
Expected output
Welcome, Diya!Run in JavaScript compiler →Predict the original program
JavaScript · original program for this question
console.log("Hello, school!");
Keep the useful idea
console.log sends a value to the host's console. Quoted text is a string, and parentheses supply that string as the function's argument. This is an instruction, not a visible HTML heading.
Check your variation against this explanation: The new string is passed to the same output operation.
Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.
Reaching the recap and completing the quizzes are separate milestones.