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

Lesson 2: Your first JavaScript program

01Opening question

If you give a computer five instructions, in what order does it carry them out?

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

Run it, then swap two lines and run it again. The output follows the order of the lines.

console.log("Step 1: boil water");
console.log("Step 2: add tea");
console.log("Step 3: enjoy");

03Objective and prerequisites

Learning objective: Write a short program of several statements, predict the order of its output, and combine strings, numbers and a stored value to produce a result.

You need: Lesson 1: What JavaScript is and where it runs

Skills practised: JavaScript basics, Values and variables.

04Explanation

A program is a list of instructions. JavaScript carries them out from the top of the file to the bottom, one at a time, unless you tell it otherwise (you will learn how in later lessons).

Text in quotes is called a string. A number without quotes is a number. The two behave differently: 2 + 3 adds to 5, but "2" + "3" joins the text into 23.

You can give a value a name so you can use it again. let score = 10; creates a box called score holding 10. Later you can put a new value in the box: score = score + 5;. Lesson 6 explores this in depth.

Programs fail in two main ways: the computer cannot read them (a typo in the grammar), or it reads them but they do the wrong thing. Testing small steps often makes both easy to spot.

05Definitions

Program
A sequence of instructions that a computer carries out.
String
A piece of text in quotes, such as "Hello".
Number
A numeric value such as 42 or 4.5, written without quotes.
Concatenation
Joining strings together, for example "Java" + "Script" gives "JavaScript".
Variable
A named box that stores a value so you can use or change it later.

Open the full glossary

06Visual learning object

Variable value explorer. Step through a short program and watch each named value change.

Loading the interactive object…

Open the full Variable value explorer page with its guided experiment.

07Examples

ex-002-01Order matters Browser-executable JavaScript

Statements run from top to bottom, so the output appears in the same order as the lines.

console.log("3");
console.log("2");
console.log("1");
console.log("Liftoff!");
Sample input
None
Expected output
3 2 1 Liftoff!
Common mistake
Thinking JavaScript sorts or rearranges output. It prints in execution order.
Try this
Move the Liftoff! line to the top and predict the new output.
Source
examples/ex-002-01-order-matters.js (repository pending publication)

ex-002-02Strings and numbers with + Browser-executable JavaScript

With two numbers, + adds. If either side is a string, + joins text instead.

console.log(2 + 3);
console.log("2" + "3");
console.log("Score: " + 10);
Sample input
None
Expected output
5 23 Score: 10
Common mistake
Expecting "2" + "3" to be 5. Quotes make them text, so they are joined.
Try this
What does "2" + 3 print? Predict, then add it and run.
Source
examples/ex-002-02-strings-and-numbers.js (repository pending publication)

ex-002-03Remember a value Browser-executable JavaScript

let creates a named value that can change. The right-hand side is worked out first, then stored.

let score = 10;
console.log("Starting score:", score);
score = score + 5;
console.log("After a bonus:", score);
Sample input
None
Expected output
Starting score: 10 After a bonus: 15
Common mistake
Writing let score twice. Declare once, then assign with score = ....
Try this
Add a penalty line that subtracts 3 and prints the result.
Source
examples/ex-002-03-remember-a-value.js (repository pending publication)

ex-002-04A tiny greeting program Browser-executable JavaScript

Constants hold values that do not change. Strings and numbers combine with + to build sentences.

const name = "Ada";
const course = "JavaScript";
console.log("Hello, " + name + "!");
console.log("Welcome to " + course + ".");
console.log(name + " has " + 3 + " lessons left today.");
Sample input
name = Ada
Expected output
Hello, Ada! Welcome to JavaScript. Ada has 3 lessons left today.
Common mistake
Forgetting spaces inside the strings, which produces Hello,Ada!
Try this
Change name to your own name and add a fourth line of your choice.
Source
examples/ex-002-04-greeting-program.js (repository pending publication)

08Guided exercises

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

Fix the countdown order

See that output follows the order of statements, then reorder them.

console.log("Liftoff!");
console.log("3");
console.log("2");
console.log("1");

Minutes to seconds

Store a calculated value in a constant and print a sentence built from values.

const minutes = 7;
// TODO: create a constant named seconds that holds minutes multiplied by 60.

// TODO: print exactly: 7 minutes = 420 seconds

Rectangle report

Write a program from requirements, using named values.

// A rectangle is 8 wide and 5 high.
// Print its area and perimeter.

09Prediction

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

let total = 5;
console.log(total);
total = total + 10;
console.log(total);
console.log("total");

What are the three lines of output?

10Debugging investigation

The order total says 2540

A checkout script copies the book price from a form field and adds the shipping cost. Customers see an order total of 2540 instead of 65.

const bookPrice = "25";   // copied from a form field
const shipping = 40;
const total = bookPrice + shipping;
console.log("Order total:", total);

More cases in the Debugging Detective Laboratory

11Practical scenario

Reading a price from a form

Values typed into web forms always arrive as strings, even when they look like numbers. A checkout that adds a typed price to shipping with + will join the text instead of adding. Converting with Number() before calculating is a habit professional developers rely on, and the Shopping Cart project's validation stage builds on it.

const typedPrice = "25";
console.log(typedPrice + 5);
console.log(Number(typedPrice) + 5);

12Knowledge check

1. What does this print?

console.log("5" + "5");
  • 10
  • 55
  • "5" + "5"
  • An error

2. What is the last line printed?

let points = 2;
points = points * 3;
points = points + 1;
console.log(points);
  • 2
  • 6
  • 7
  • 9

3. In what order do the statements of a simple program run?

  • Alphabetical order
  • Top to bottom, one after another
  • All at the same time
  • Bottom to top

4. This should print Hello, Sam! but prints Hello,Sam!. What is wrong?

const name = "Sam";
console.log("Hello," + name + "!");
  • name should be in quotes
  • The string "Hello," needs a space at the end
  • + cannot join strings
  • console.log needs commas instead

5. Which line prints Total: 30 using the values a = 10 and b = 20?

  • console.log("Total: a + b");
  • console.log("Total:", a + b);
  • console.log("Total:" + a + b);
  • console.log(Total: a + b);

6. What does score = score + 5; do?

  • Checks whether score equals score + 5
  • Reads score, adds 5, and stores the result back in score
  • Creates a second variable named score
  • Prints score + 5

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.

  • Fix the countdown order: Counts down, then lifts off
  • Minutes to seconds: Prints the sentence; seconds holds 420 (extra case)
  • Rectangle report: Prints area and perimeter; width is 8 and height is 5; Values are calculated from width and height
  • ex-002-01: output matches the documented expected output
  • ex-002-02: output matches the documented expected output
  • ex-002-03: output matches the documented expected output
  • ex-002-04: 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-002-first-javascript-program/. Every example on this page is generated from those files.

The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.

15Summary and next steps

  • Programs run top to bottom, one statement at a time.
  • + adds numbers but joins strings; quotes change the meaning.
  • let creates a value that can change; const creates one that cannot be reassigned.
  • Build programs in small steps and run often.

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 Strings versus numbers
Context: I am learning that + behaves differently for strings and numbers.
Task: Explain why "2" + 3 gives "23" while 2 + 3 gives 5.
Constraints: Use at most four short code lines. Avoid the words coercion and primitive unless you define them.
Expected behaviour: I can predict the result of + for any mix of strings and numbers.
Testing: Give me four mixed examples to predict, with answers at the end.
Code prediction Sequence drill
Context: I know that statements run top to bottom.
Task: Write a six-line program using let, reassignment and console.log, and ask me to predict the full output.
Constraints: Only numbers, strings, let, const, + and *. Do not reveal the answer until I reply.
Expected behaviour: The output has four lines.
Testing: Show the correct output and trace each line after I answer.
Guided learning Build a greeting program
Context: I can use console.log and const.
Task: Guide me to write a program that prints a three-line greeting using two constants.
Constraints: Give one instruction at a time and wait for my code. Point out mistakes with questions, not corrections.
Expected behaviour: My program prints three greeting lines built with +.
Testing: Ask me to run my code and paste the output after each step.
Testing Check my output
Context: I wrote a program that should print `7 minutes = 420 seconds`.
Task: List what could make my output differ slightly from the expected text.
Constraints: Focus on spaces, capitals and quotes. Do not rewrite my code.
Expected behaviour: I can check my own output character by character.
Testing: Give me a checklist I can tick off.
Refactoring Replace repeated numbers
Context: My program repeats the number 60 and the number 7 in several places.
Task: Show me how storing them in named constants makes the program easier to change.
Constraints: Keep my program's output identical. Explain each change in one sentence.
Expected behaviour: Changing one constant updates every result.
Testing: Show the output before and after to prove it is unchanged.