⬅ Previous Topic
Set Up JavaScript EnvironmentNext Topic ⮕
JavaScript in the Browser vs Node.js⬅ Previous Topic
Set Up JavaScript EnvironmentNext Topic ⮕
JavaScript in the Browser vs Node.jsEvery 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.
This is the fastest and most accessible way to run JavaScript without any installation.
Enter
:
console.log("Hello, World!");
Hello, World!
Explanation: console.log()
is a JavaScript function used to print messages to the console — great for debugging or quick testing.
Node.js is a JavaScript runtime that allows you to run JS outside the browser, typically for backend development.
hello.js
using a code editor or touch hello.js
on terminal.
// hello.js
console.log("Hello, World from Node.js!");
node hello.js
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.
VS Code is a lightweight, powerful code editor perfect for writing and testing JavaScript projects.
HelloWorldProject
).hello.js
.
// hello.js
console.log("Hello from Visual Studio Code!");
Ctrl + `
).
node hello.js
Hello from Visual Studio Code!
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.
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.
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>
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.Yes, by using Node.js or an online editor like Repl.it or JSFiddle.
No, but it's useful if you want to run JS outside the browser or build server-side applications.
alert()
work in Node.js?Because alert()
is part of the browser's window
object and Node.js doesn't have a DOM.
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!
⬅ Previous Topic
Set Up JavaScript EnvironmentNext Topic ⮕
JavaScript in the Browser vs Node.jsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.