JavaScript AcademyProfessional Engineering

JavaScript reference

An educational reference for the language features and browser APIs used in the academy, with runnable examples. It grows with each lesson; for the complete language, follow the MDN and ECMAScript links.

Language

console.log() Console

Prints one or more values to the console at the log level.

Syntax
console.log(value1, value2, /* …, */ valueN)
Parameters
Any number of values of any type. They are printed separated by spaces.
Returns
undefined
Example
const item = "Notebook";
console.log("Item:", item, "x", 3);

Expected output:

Item: Notebook x 3
Common mistakes
Joining an object with + hides its contents: use a comma instead.
Forgetting that console.log returns undefined, so const x = console.log(1) stores undefined.
Practical use
Labelled logging of intermediate values while investigating a wrong result.
Lessons
Lesson 1: What JavaScript is and where it runs, Lesson 4: Using the console and understanding output
Official docs
MDN Web Docs · ECMAScript specification

console.warn() and console.error() Console

Print messages at the warning and error levels, shown in distinct colours and often collected by monitoring tools.

Syntax
console.warn(...values)
console.error(...values)
Parameters
Any values, like console.log.
Returns
undefined
Example
console.warn("Only", 2, "left");
console.error("Payment failed");

Expected output:

Only 2 left Payment failed
Common mistakes
Using error for routine messages, which buries real problems.
Practical use
Flagging low stock or a failed payment in a checkout.
Lessons
Lesson 4: Using the console and understanding output
Official docs
MDN Web Docs · ECMAScript specification

console.table() Console

Displays an array or object as a table of rows and columns.

Syntax
console.table(data)
Parameters
data: an array or object (usually an array of records).
Returns
undefined
Example
console.table([{ name: "A", price: 25 }, { name: "B", price: 40 }]);

Expected output:

│ (index) │ name │ price │ ├─────────┼──────┼───────┤ │ 0 │ "A" │ 25 │ │ 1 │ "B" │ 40 │
Common mistakes
Expecting it to sort or total the data.
Practical use
Checking a list of cart lines at a glance.
Lessons
Lesson 4: Using the console and understanding output
Official docs
MDN Web Docs · ECMAScript specification

console.group() and console.groupEnd() Console

Indent the console lines between the two calls, so a step-by-step trace stays readable.

Syntax
console.group(label)
// …
console.groupEnd()
Parameters
label (optional): printed at the start of the group.
Returns
undefined
Example
console.group("Checkout");
console.log("subtotal:", 90);
console.groupEnd();
console.log("done");

Expected output:

Checkout subtotal: 90 done
Common mistakes
Forgetting groupEnd, leaving every later line indented.
Practical use
Tracing the steps of a calculation.
Lessons
Lesson 4: Using the console and understanding output
Official docs
MDN Web Docs · ECMAScript specification

let and const Statements and declarations

Declare block-scoped variables. let can be reassigned; const cannot.

Syntax
let name = value;
const name = value;
Parameters
name: an identifier. value: any expression (required for const).
Returns
Not an expression: declarations produce no value.
Example
let score = 10;
score = score + 5;
const bonus = 2;
console.log(score + bonus);

Expected output:

17
Common mistakes
Reassigning a const throws TypeError: Assignment to constant variable.
Declaring the same name twice in one block is a SyntaxError.
Practical use
const for prices and rates, let for a running total.
Lessons
Lesson 2: Your first JavaScript program, Lesson 6: Variables with let and const
Official docs
MDN Web Docs · ECMAScript specification

String literals Data types

Text values written in double quotes, single quotes or backticks.

Syntax
"text"   'text'   `text ${expression}`
Parameters
None.
Returns
A string value.
Example
const a = "Java";
const b = 'Script';
console.log(a + b);
console.log(`${a}${b} has ${(a + b).length} letters`);

Expected output:

JavaScript JavaScript has 10 letters
Common mistakes
Mismatched quotes cause SyntaxError: Unterminated string constant.
Numbers in quotes are text: "2" + "3" is "23".
Practical use
Messages, labels and product names.
Lessons
Lesson 2: Your first JavaScript program, Lesson 7: Strings and template literals
Official docs
MDN Web Docs · ECMAScript specification

Arithmetic operators: + - * / % ** Operators

Calculate with numbers: addition, subtraction, multiplication, division, remainder and exponent.

Syntax
a + b   a - b   a * b   a / b   a % b   a ** b
Parameters
Two operands (numbers; other types are converted).
Returns
A number (or a string for + with a string operand).
Example
console.log(7 + 8 * 2);
console.log((7 + 8) * 2);
console.log(10 % 4, 2 ** 3);

Expected output:

23 30 2 8
Common mistakes
Forgetting precedence: * and / happen before + and -.
Expecting exact decimals: 0.1 + 0.2 is 0.30000000000000004.
Practical use
Line totals, discounts and tax.
Lessons
Lesson 3: Statements, expressions and comments, Lesson 8: Numbers and mathematical operations
Official docs
MDN Web Docs · ECMAScript specification

The + operator with strings Operators

Adds numbers, but joins text when either side is a string.

Syntax
a + b
Parameters
Two operands.
Returns
A number or a string.
Example
console.log(2 + 3);
console.log("2" + 3);
console.log("Total: " + (2 + 3));

Expected output:

5 23 Total: 5
Common mistakes
Adding form input (always text) without Number() first.
Practical use
Building sentences; avoiding the "2540" checkout bug.
Lessons
Lesson 2: Your first JavaScript program
Official docs
MDN Web Docs · ECMAScript specification

Grouping operator ( ) Operators

Parentheses make the enclosed expression evaluate first, overriding precedence.

Syntax
(expression)
Parameters
Any expression.
Returns
The value of the expression.
Example
const average = (80 + 90) / 2;
console.log(average);

Expected output:

85
Common mistakes
Leaving them out in averages and discounts.
Practical use
Correct averages, discounts and tax calculations.
Lessons
Lesson 3: Statements, expressions and comments
Official docs
MDN Web Docs · ECMAScript specification

Comments Lexical grammar

Notes for humans that JavaScript ignores.

Syntax
// to the end of the line
/* across
   several lines */
Parameters
None.
Returns
Nothing: comments are removed before running.
Example
// switched off: console.log("debug");
console.log("shown"); // trailing comment

Expected output:

shown
Common mistakes
An unclosed /* comments out the rest of the file.
Practical use
Explaining why a rule exists; switching lines off while debugging.
Lessons
Lesson 3: Statements, expressions and comments
Official docs
MDN Web Docs · ECMAScript specification

typeof Operators

Returns a string naming the type of a value.

Syntax
typeof value
Parameters
Any value or expression.
Returns
"number", "string", "boolean", "undefined", "object", "function", "bigint" or "symbol".
Example
console.log(typeof 42, typeof "42", typeof true);
console.log(typeof null);

Expected output:

number string boolean object
Common mistakes
typeof null is "object" (a historical quirk).
typeof [] is "object": use Array.isArray.
Practical use
Checking whether a value from a form is still a string.
Lessons
Lesson 1: What JavaScript is and where it runs, Lesson 10: Type conversion and type checking
Official docs
MDN Web Docs · ECMAScript specification

Error types: SyntaxError, ReferenceError, TypeError, RangeError Error objects

Built-in error objects describing what went wrong. Each has a name, a message and a stack.

Syntax
new Error(message)
throw new RangeError("Quantity must be 0–99")
Parameters
message: a description of the problem.
Returns
An Error object.
Example
try {
  null.length;
} catch (error) {
  console.log(error.name);
  console.log(error instanceof TypeError);
}

Expected output:

TypeError true
Common mistakes
Fixing the symptom (declaring a misspelled name) instead of the cause.
Ignoring the line number.
Practical use
Reading the console; throwing clear errors for invalid input (Shopping Cart Stage 10).
Lessons
Lesson 5: Reading errors and developing a programming mindset
Official docs
MDN Web Docs · ECMAScript specification

Math.max() and Math.min() Math

Return the largest or smallest of the numbers given.

Syntax
Math.max(a, b, ...)
Math.min(a, b, ...)
Parameters
Zero or more numbers.
Returns
A number (−Infinity / Infinity with no arguments; NaN if any argument is NaN).
Example
console.log(Math.max(78, 85, 89));
console.log(Math.min(78, 85, 89));
console.log(Math.min(99, 105));

Expected output:

89 78 99
Common mistakes
Passing an array directly: use Math.max(...array).
Practical use
Highest score; capping a quantity at 99.
Lessons
Lesson 5: Reading errors and developing a programming mindset
Official docs
MDN Web Docs · ECMAScript specification

Math.round(), Math.ceil(), Math.floor() Math

Round to the nearest whole number, up, or down.

Syntax
Math.round(x)   Math.ceil(x)   Math.floor(x)
Parameters
x: a number.
Returns
A whole number.
Example
console.log(Math.round(2.5), Math.ceil(2.1), Math.floor(2.9));
console.log(Math.round(0.1 * 3 * 100) / 100);

Expected output:

3 3 2 0.3
Common mistakes
Rounding money only at the end, letting floating-point noise appear in intermediate values.
Practical use
Rounding money to cents; splitting a bill (Tip Calculator).
Lessons
Lesson 8: Numbers and mathematical operations
Official docs
MDN Web Docs · ECMAScript specification

Number() and Number.isInteger() Numbers

Number(text) converts a value to a number; Number.isInteger checks for a whole number.

Syntax
Number(value)
Number.isInteger(value)
Number.isFinite(value)
Parameters
value: anything (strings are parsed).
Returns
A number (NaN if the text is not numeric) / a boolean.
Example
console.log(Number("25") + 40);
console.log(Number("ten"));
console.log(Number.isInteger(2.5), Number.isInteger(3));

Expected output:

65 NaN false true
Common mistakes
Number("") is 0, not NaN: check for empty text separately.
Practical use
Validating form input (Tip Calculator, Shopping Cart Stage 8).
Lessons
Lesson 10: Type conversion and type checking
Official docs
MDN Web Docs · ECMAScript specification

Browser APIs

document.querySelector() DOM

Returns the first element that matches a CSS selector, or null.

Syntax
document.querySelector(selector)
Parameters
selector: a CSS selector string such as "#total" or ".item".
Returns
An Element or null.
Example
const el = document.querySelector("#status");
el.textContent = "Ready";
console.log(el.textContent);

Expected output:

Ready
Common mistakes
Forgetting # for ids or . for classes.
Running the script before the element exists.
Practical use
Finding the total element in the Shopping Cart.
Lessons
Lesson 1: What JavaScript is and where it runs, Lesson 52: Selecting elements planned
Official docs
MDN Web Docs

Node.textContent DOM

Reads or replaces the text inside an element. Assigned text is never parsed as HTML, which makes it safe for user data.

Syntax
element.textContent = "new text"
Parameters
A string when assigning.
Returns
The element's text.
Example
const p = document.querySelector("p");
p.textContent = "<b>not bold</b>";
console.log(p.children.length);

Expected output:

0
Common mistakes
Using innerHTML for data, which can execute injected markup (XSS).
Practical use
Rendering product names safely.
Lessons
Lesson 53: Reading and changing content planned, Lesson 122: Cross-site scripting and unsafe DOM operations planned
Official docs
MDN Web Docs

document.createElement() and append() DOM

Create a new element and insert it into the page.

Syntax
const el = document.createElement("li");
parent.append(el);
Parameters
tagName: the element type.
Returns
The new element (append returns undefined).
Example
const list = document.querySelector("ul");
for (const name of ["A", "B"]) {
  const li = document.createElement("li");
  li.textContent = "Product " + name;
  list.append(li);
}
console.log(list.children.length);

Expected output:

2
Common mistakes
Creating the element but never appending it.
Practical use
Building cart rows (Shopping Cart Stage 5).
Lessons
Lesson 54: Creating and removing elements planned
Official docs
MDN Web Docs

EventTarget.addEventListener() Events

Runs a function whenever an event such as click or input happens on an element.

Syntax
element.addEventListener("click", (event) => { /* … */ })
Parameters
type: the event name. listener: the function to call.
Returns
undefined
Example
const b = document.querySelector("button");
let clicks = 0;
b.addEventListener("click", () => { clicks++; console.log("clicks:", clicks); });
b.click();
b.click();

Expected output:

clicks: 1 clicks: 2
Common mistakes
Calling the function instead of passing it: addEventListener("click", handle()) runs handle immediately.
Adding the same listener on every render, which duplicates handlers.
Practical use
Add and Remove buttons in the Shopping Cart (Stage 6).
Lessons
Lesson 56: Event listeners planned, Lesson 59: Event delegation planned
Official docs
MDN Web Docs

localStorage Storage

Stores text under keys for this site, and keeps it after the page closes.

Syntax
localStorage.setItem(key, text)
localStorage.getItem(key)
localStorage.removeItem(key)
Parameters
key and value are strings.
Returns
getItem returns the string or null.
Example
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme"));
console.log(localStorage.getItem("missing"));

Expected output:

dark null
Common mistakes
Storing objects without JSON.stringify (they become "[object Object]").
Trusting stored data without validating it.
Practical use
Keeping the Shopping Cart after a reload (Stage 9). In the academy sandbox this is a memory-only stand-in.
Lessons
Lesson 68: Local storage and session storage planned
Official docs
MDN Web Docs

JSON.stringify() and JSON.parse() Data

Convert values to JSON text and back.

Syntax
JSON.stringify(value)
JSON.parse(text)
Parameters
value: data to convert; text: a JSON string.
Returns
A string / the parsed value. JSON.parse throws SyntaxError on invalid text.
Example
const text = JSON.stringify({ id: "A", quantity: 2 });
console.log(text);
console.log(JSON.parse(text).quantity);

Expected output:

{"id":"A","quantity":2} 2
Common mistakes
Calling JSON.parse on corrupted text without try/catch.
Practical use
Saving and loading the cart.
Lessons
Lesson 69: JSON serialization planned
Official docs
MDN Web Docs