Module 2: Setting Up Your Environment
Objective: Learn how to set up a proper environment to write, test, and run JavaScript code, including using the browser console and a text editor.
1. Writing JavaScript in the Browser Console
The browser console is the simplest place to experiment with JavaScript.
Steps to Access the Console:
- Open your browser (e.g., Chrome, Firefox, or Edge).
- Right-click anywhere on a webpage and choose Inspect (or press
Ctrl + Shift + I
/Cmd + Option + I
). - Click the Console tab in the developer tools.
Try This:
Type this into the console and press Enter:
console.log("JavaScript is awesome!");
You should see the message printed in the console.
2. Using a Text Editor
To write and save JavaScript code for larger projects, you’ll need a text editor. One of the most popular and beginner-friendly editors is Visual Studio Code (VS Code).
Steps to Install VS Code:
- Go to VS Code's official website and download it for your operating system.
- Install VS Code by following the instructions.
- Once installed, open the application.
3. Writing JavaScript in VS Code
Let’s create your first JavaScript file.
Steps:
- Open VS Code.
- Create a new folder on your computer where you want to save your JavaScript files (e.g.,
MyJavaScriptProjects
). - Inside VS Code, click File > Open Folder, and select the folder you just created.
- Create a new file by clicking File > New File and save it as
app.js
(the.js
extension tells the computer it’s a JavaScript file).
Writing Code:
In app.js
, write this:
let message = "Hello, JavaScript!"; console.log(message);
4. Running Your JavaScript File
You can run your app.js
file in two ways: directly in the browser or using Node.js (a JavaScript runtime).
Option 1: Running JavaScript in the Browser
-
Create an HTML file to load your JavaScript.
-
Save a new file in the same folder as
app.js
and name itindex.html
. -
Write this code in
index.html
:<!DOCTYPE html> <html> <head> <title>My First JavaScript</title> </head> <body> <script src="app.js"></script> </body> </html>
-
Open
index.html
in your browser by double-clicking the file. -
Open the console to see the output.
Option 2: Running JavaScript with Node.js
-
Install Node.js from nodejs.org (download the LTS version).
-
Open a terminal or command prompt.
-
Navigate to the folder where your
app.js
file is saved:cd path/to/your/folder
-
Run the file using Node.js:
node app.js
-
You should see
Hello, JavaScript!
printed in the terminal.
5. Understanding How the Browser Reads JavaScript
When the browser loads a webpage:
- It reads the HTML for structure.
- It applies CSS for styling.
- It runs JavaScript for interactivity.
The <script>
tag in index.html
tells the browser to load and execute the app.js
file.
Example:
If you add this to app.js
:
alert("Welcome to JavaScript!");
When you open index.html
, you’ll see a pop-up alert.
6. Practice Exercise
Let’s make sure you’re comfortable with your setup.
Task 1:
-
Create a new JavaScript file called
practice.js
. -
Write a program to print:
console.log("I just set up my JavaScript environment!");
Task 2:
- Modify your
index.html
file to includepractice.js
instead ofapp.js
. - Reload
index.html
in the browser and check the console for the message.
Challenge:
-
Add another
<script>
tag toindex.html
for a second JavaScript file (e.g.,extra.js
). -
Write a small program in
extra.js
to calculate and display the sum of two numbers:let a = 5; let b = 10; console.log("The sum is: " + (a + b));