JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 1: Discovering JavaScriptLesson 1 of 150About 35 minutes

Lesson 1: What JavaScript is and where it runs

01Opening question

When you change a quantity in an online shop and the total updates instantly, without the page reloading, what is doing that work?

Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.

02Start with working code

Press Run Code. Two lines appear in the console: the text, then the number 5.

console.log("Hello! JavaScript is running.");
console.log(2 + 3);

03Objective and prerequisites

Learning objective: Explain what JavaScript is, name the places it runs (browsers, Node.js and other hosts) and run your first lines of JavaScript in a safe environment.

You need: None. A modern browser is all you need.

Skills practised: JavaScript basics, Console output.

04Explanation

JavaScript is a programming language: a precise way to give a computer instructions. You write the instructions as text, and a program called a JavaScript engine reads them and carries them out, one after another.

Every modern web browser contains a JavaScript engine. That is why JavaScript is the language of interactive websites: it reacts when you click, checks a form before it is sent, updates a shopping-cart total or draws a chart.

JavaScript is not limited to browsers. Node.js runs JavaScript on servers and laptops, where it can read files and answer web requests. Level 5 of this academy uses Node.js, and those lessons come with projects you run on your own computer.

When you press Run Code in a lesson, your code runs in a sandbox: a sealed-off area of your browser. Your code can calculate and print results, but it cannot see this page or your saved progress, so you can experiment freely.

05Definitions

JavaScript
A programming language used to make web pages interactive and, with Node.js, to build servers and tools.
JavaScript engine
The program inside a browser or Node.js that reads JavaScript code and carries it out.
Host environment
The place JavaScript runs (a browser, Node.js, a worker). It adds extra capabilities such as document or file access.
Node.js
A program that runs JavaScript outside the browser, on servers and on your own computer.
Console
A text area where a program can print messages with console.log. Developers use it to see results and find problems.
Sandbox
An isolated area where code runs without access to the rest of the page or your data.

Open the full glossary

06Visual learning object

Where does this code run?. Sort each capability: does it belong to the JavaScript language (available everywhere), the browser host or the Node.js host?

Loading the interactive object…

07Examples

ex-001-01Your first output Browser-executable JavaScript

console.log prints whatever you put between its parentheses. Each call prints one line, in the order the lines appear.

// console.log prints a value to the console.
console.log("JavaScript is running.");
console.log("This line runs second.");
Sample input
None
Expected output
JavaScript is running. This line runs second.
Common mistake
Forgetting the quotes around text: console.log(Hello) looks for something called Hello and fails.
Try this
Add a third line that prints your own message.
Source
examples/ex-001-01-first-output.js

ex-001-02JavaScript calculates Browser-executable JavaScript

Numbers and arithmetic operators outside quotes are calculated. Multiplication happens before addition, as in ordinary maths.

// JavaScript works out values for you.
console.log(25 * 3);
console.log(100 - 37);
console.log(7 + 8 * 2);
Sample input
None
Expected output
75 63 23
Common mistake
Expecting 7 + 8 * 2 to be 30. Multiplication is done first, so it is 7 + 16.
Try this
Change the last line so it prints 30 by adding parentheses.
Source
examples/ex-001-02-javascript-calculates.js

ex-001-03One language, many hosts Browser-executable JavaScript

These lines use only the JavaScript language itself, so they behave the same in a browser, a worker and Node.js.

// The language core is the same everywhere.
console.log(typeof 42);
console.log(typeof "text");
console.log("Math works everywhere:", Math.max(4, 9, 2));
Sample input
None
Expected output
number string Math works everywhere: 9
Common mistake
Assuming document exists everywhere. It is part of the browser host, not the language.
Try this
Add console.log(Math.min(4, 9, 2)); and predict the output first.
Source
examples/ex-001-03-same-language-everywhere.js

ex-001-04JavaScript changes a page Browser page (sandboxed preview)

In a browser page, JavaScript can find an element and change its text. The preview below runs this code in a sandboxed page.

// In a browser page, JavaScript can change what you see.
const status = document.querySelector("#status");
status.textContent = "JavaScript changed this sentence.";
console.log("Updated:", status.textContent);
Sample input
The HTML file contains a paragraph with id status.
Expected output
Updated: JavaScript changed this sentence.

The paragraph under Welcome reads: JavaScript changed this sentence.

Common mistake
Forgetting the # in #status: the # means 'the element whose id is status'.
Try this
Change the heading too: select #title and give it new text.
Source
examples/ex-001-04-javascript-changes-a-page.js

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Change the message

Edit working code and see your change in the output.

console.log("Hello from the starter code");
console.log(10 + 5);

Complete the ticket total

Finish a program by printing a calculated total.

const ticketPrice = 12;
const tickets = 4;

// TODO: print "Total:" followed by the cost of all the tickets.
// Use the two values above instead of typing the answer.

Print a café receipt

Write a complete small program from a description.

// A coffee costs 4 and a muffin costs 3.
// Write a program that prints a three-line receipt.

09Prediction

Commit to an answer first, then run the real code to compare.

console.log("A");
console.log(2 * 5);
console.log("2 * 5");

What does this program print, line by line?

10Debugging investigation

The welcome banner never appears

The store's start-up script should print a welcome banner and today's special. When it runs, the console shows only a red error and neither line appears, not even the second line, which looks correct.

console.log("Welcome to the JavaScript store);
console.log("Today's special: 3 for 2");

More cases in the Debugging Detective Laboratory

11Practical scenario

A bakery order form

A small online bakery wants its order form to show the price as customers change quantities. JavaScript in the page can recalculate the total instantly, without reloading. Later, a Node.js server could store each order. You will build exactly this kind of calculator in the Shopping Cart project, starting with plain calculations in Stage 1.

const croissant = 3;
const quantity = 4;
console.log("Order total:", croissant * quantity);

12Knowledge check

1. What does this line print?

console.log(3 + 4);
  • 3 + 4
  • 7
  • 34
  • undefined

2. Which part of a browser reads and carries out JavaScript instructions?

  • The JavaScript engine
  • The HTML parser
  • The operating system's file manager
  • The CSS renderer

3. Which of these comes from the browser host, not from the JavaScript language itself?

  • Numbers like 42
  • The + operator
  • document.querySelector
  • Strings like "hi"

4. Why does this line fail?

console.log("Total: " 25);
  • console.log can only print text
  • A comma (or +) is missing between the two values
  • 25 must be in quotes
  • The line needs a semicolon

5. Where can JavaScript run?

  • Only in web browsers
  • Only on servers
  • In browsers, in Node.js and in other hosts
  • Only inside HTML files

6. You need a script, started from the terminal, that renames files on your computer. Which environment fits?

  • A browser page
  • Node.js
  • CSS
  • The sandbox in a lesson

13Solutions and tests

Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.

Automated acceptance tests for this lesson

These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.

  • Change the message: Prints the two expected lines; 144 is calculated, not typed
  • Complete the ticket total: Prints Total: 48; Uses ticketPrice and tickets in the calculation
  • Print a café receipt: Prints the three receipt lines; The total uses addition; Runs without errors
  • ex-001-01: output matches the documented expected output
  • ex-001-02: output matches the documented expected output
  • ex-001-03: output matches the documented expected output

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

The lesson lives in the companion repository at curriculum/level-01-foundations/module-01-discovering-javascript/lesson-001-what-is-javascript/. Every example on this page is generated from those files.

These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.

15Summary and next steps

  • JavaScript is a language; an engine runs it.
  • The host (browser, Node.js, worker) decides what extra features exist, such as document or file access.
  • console.log prints values; text in quotes is printed as written, expressions are calculated first.
  • Lesson code runs in a sandbox, so experimenting is safe.

16AI learning prompts

Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.

Concept explanation Explain hosts with an analogy
Context: I am a complete beginner in Lesson 1 of a JavaScript course.
Task: Explain the difference between the JavaScript language and the browser or Node.js host, using one everyday analogy.
Constraints: Under 150 words. No code longer than two lines. Do not introduce frameworks.
Expected behaviour: I can say which of console.log, document and fs belongs to which environment.
Testing: Finish with three quick questions I can answer to check my understanding, with answers hidden at the end.
Code prediction Quiz me on output
Context: I can use console.log with strings and arithmetic.
Task: Give me five one-line console.log statements and ask me to predict each output.
Constraints: Use only strings, numbers, + - * / and parentheses. Wait for my answers before revealing results.
Expected behaviour: Each item has one exact output.
Testing: After I answer, show the real output and explain any I got wrong.
Guided learning Walk me through running code
Context: I have never run code before.
Task: Guide me step by step to run `console.log("Hello")` in my browser's developer console.
Constraints: Assume Chrome or Firefox on a laptop. One step per message; wait for me to confirm.
Expected behaviour: I see Hello printed in the console.
Testing: Ask me to describe what I see after each step.
Exercise generation More practice like Exercise C
Context: I finished an exercise that prints a three-line café receipt with a calculated total.
Task: Create two similar exercises with different items and prices.
Constraints: Only console.log, strings, numbers and +. Provide expected output for each.
Expected behaviour: Each exercise has exactly one correct output.
Testing: Include the expected output so I can compare my result.
Debugging Help me read a syntax error
Context: My code shows `SyntaxError: Unterminated string constant`.
Task: Explain what the message means and how to find the problem without rewriting my code for me.
Constraints: Ask me to paste the line number reported. Give hints, not the corrected line, unless I ask twice.
Expected behaviour: I can find and fix the missing quote myself.
Testing: Ask me to rerun the code and tell you the new output.