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.
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.
A script is parsed completely before execution begins, so a syntax error anywhere prevents every statement from running. After parsing, statements run sequentially and synchronously in source order.
The + operator is overloaded: after converting both operands to primitives, if either is a string it performs string concatenation, otherwise numeric addition. This is a common source of bugs when values come from user input, which is always text.
let and const create block-scoped bindings. const prevents reassignment of the binding (not mutation of an object it refers to). Prefer const and use let only for values that change.
The academy wraps your code in an async function running in strict mode, so top-level await works in later lessons. For the code in this level the behaviour matches an ordinary strict-mode script.
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.
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.
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
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.
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 explanationStrings 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 predictionSequence 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 learningBuild 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.
TestingCheck 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.
RefactoringReplace 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.