3. Setting Up Your Environment

Prerequisites

Before we start coding, let's set up your development environment. You'll need:

  1. A text editor or IDE
  2. Node.js (for running JavaScript outside the browser)
  3. A web browser (for testing)

Installing Node.js

Node.js allows you to run JavaScript on your computer.

Windows/macOS/Linux

  1. Visit nodejs.org
  2. Download the LTS (Long Term Support) version
  3. Run the installer
  4. Verify installation:
node --version
npm --version

Choosing a Code Editor

Visual Studio Code (Recommended)

  1. Download from code.visualstudio.com
  2. Install useful extensions:
    • ES6 Mocha Snippets
    • JavaScript (ES6) code snippets
    • Prettier (code formatter)
    • ESLint (code linting)

Other Options

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:

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.