3. Setting Up Your Environment
Prerequisites
Before we start coding, let's set up your development environment. You'll need:
- A text editor or IDE
- Node.js (for running JavaScript outside the browser)
- A web browser (for testing)
Installing Node.js
Node.js allows you to run JavaScript on your computer.
Windows/macOS/Linux
- Visit nodejs.org
- Download the LTS (Long Term Support) version
- Run the installer
- Verify installation:
node --version
npm --version
Choosing a Code Editor
Visual Studio Code (Recommended)
- Download from code.visualstudio.com
- Install useful extensions:
- ES6 Mocha Snippets
- JavaScript (ES6) code snippets
- Prettier (code formatter)
- ESLint (code linting)
Other Options
- Sublime Text
- Atom
- WebStorm
- Vim/Emacs
Setting Up a Project
Create a new directory for your ECMAScript projects:
mkdir ecmascript-projects
cd ecmascript-projects
Initialize a Node.js project:
npm init -y
This creates a package.json file.
Running JavaScript
In Node.js
Create a file called hello.js:
console.log("Hello, ECMAScript!");
Run it:
node hello.js
In the Browser
Create an HTML file:
<!DOCTYPE html>
<html>
<head>
<title>ECMAScript Tutorial</title>
</head>
<body>
<h1>Hello, ECMAScript!</h1>
<script>
console.log("Hello from the browser!");
</script>
</body>
</html>
Open in your browser and check the console (F12).
Using the Browser Console
Modern browsers have built-in developer tools:
- Chrome/Edge: Press F12 or Ctrl+Shift+I
- Firefox: Press F12 or Ctrl+Shift+K
- Safari: Enable developer tools in preferences, then press Cmd+Option+C
You can run JavaScript directly in the console for quick testing.
Online Playgrounds
If you don't want to set up locally:
Version Control (Optional but Recommended)
Install Git:
# Linux
sudo apt install git
# macOS
brew install git
# Windows: Download from git-scm.com
Initialize a Git repository:
git init
Testing Your Setup
Create a test file to verify everything works:
// test.js
const message = "ECMAScript is working!";
console.log(message);
// Test ES6 features
const arrowFunction = () => "Arrow functions work!";
console.log(arrowFunction());
const promise = new Promise((resolve) => resolve("Promises work!"));
promise.then(console.log);
Run with node test.js.
Next Steps
Your environment is ready! Let's dive into the basic syntax of ECMAScript.