1. Introduction
Before writing JavaScript programs, you need a way to run them. The simplest environment is any modern web browser, which has a built-in JavaScript engine accessible through the Developer Console. For more serious development, most developers install Node.js, a JavaScript runtime that lets you run .js files directly from the command line, outside a browser.
Cricket analogy: The browser console is like a quick net session in your backyard to test a technique, while Node.js is like joining a proper academy — a dedicated setup where you can run full practice sessions (.js files) outside the casual backyard (browser) environment.
A typical setup also includes a code editor (such as Visual Studio Code) and, for larger projects, a package manager like npm (bundled with Node.js) to install and manage third-party libraries.
Cricket analogy: A code editor like VS Code is your training kit, and for a full squad-level operation you also need npm, the equipment manager bundled with Node.js, to source and manage third-party gear (libraries) instead of making everything yourself.
2. Syntax
// hello.js
console.log("Hello from Node.js!");
// Run it from a terminal with:
// node hello.js
// Check installed versions:
// node --version
// npm --version3. Explanation
To set up a basic JavaScript environment: (1) download and install Node.js from nodejs.org, which includes the 'node' runtime and the 'npm' package manager; (2) install a code editor like VS Code; (3) create a file ending in .js; (4) run it with the command 'node filename.js' in a terminal. Alternatively, for quick experiments without installing anything, you can open any browser's Developer Tools (F12) and use the Console tab directly.
Cricket analogy: Setting up is like: (1) join an academy — install Node.js, getting the runtime coach and npm equipment manager together, (2) get your kit (editor), (3) prepare a training plan (.js file), (4) run the session with 'node filename.js' — or for a quick net session, just grab a bat in the backyard (browser DevTools, F12, Console tab).
npm (Node Package Manager) comes bundled with Node.js and lets you install reusable packages, for example by running 'npm install <package-name>' inside a project folder that has a package.json file, created via 'npm init'.
Cricket analogy: npm is the team's equipment supplier bundled with the academy (Node.js) — you set up a squad roster with 'npm init' (creating package.json) then bring in specific gear with 'npm install <package-name>', like ordering a specific brand of bat for the squad.
A common gotcha for beginners: code run in a browser's console has access to browser-only globals like 'window' and 'document', while code run with Node.js does not have these by default and instead has access to Node-only globals like 'process' and 'require'/'module'. Mixing them up (for example, trying to use 'document.getElementById' inside a plain Node.js script) will throw a ReferenceError.
4. Example
// app.js
const os = require("os");
function describeEnvironment() {
return `Node.js is running on platform: ${process.platform}`;
}
console.log(describeEnvironment());
console.log("CPU cores available:", os.cpus().length > 0);5. Output
Node.js is running on platform: linux
CPU cores available: true6. Key Takeaways
- Any modern browser's Developer Console can run JavaScript instantly, with no installation required.
- Node.js is a runtime that executes JavaScript outside the browser, typically via the terminal.
- Installing Node.js also installs npm, the standard package manager for JavaScript libraries.
- Run scripts with 'node filename.js'; check tooling with 'node --version' and 'npm --version'.
- Browser scripts and Node.js scripts have different global objects (window/document vs. process/require).
- A code editor like VS Code, with syntax highlighting and debugging support, greatly improves productivity.
Practice what you learned
1. Which tool lets you run JavaScript files directly from the terminal, outside a browser?
2. What command runs a file named app.js using Node.js?
3. What is npm primarily used for?
4. Why would 'document.getElementById(...)' throw a ReferenceError inside a plain Node.js script?
5. Without installing anything, where can you quickly test small JavaScript snippets?
Was this page helpful?
You May Also Like
Introduction to JavaScript Programming
An overview of what JavaScript is, why it exists, and how it powers interactive behavior on the web and beyond.
Console and Debugging in JavaScript
Learn the core console methods and debugging techniques for inspecting and troubleshooting JavaScript code.
Modules in JavaScript (import/export)
How ES modules split code into reusable files using import and export, and how they differ from CommonJS.
Variables in JavaScript (var, let, const)
Learn how var, let, and const differ in scope, hoisting, and reassignment when declaring variables in JavaScript.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics