All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Adding JavaScript to a page: comments, variables, assignment and operators

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

JavaScript makes pages interactive. Start with where scripts go, comments, variables, data types, assignment and operators (spec 9.3.1 and 9.3.2 a, b and g).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Adding JavaScript (9.3.1)

<!-- external file: best practice, shared by many pages -->
<script src="js/main.js" defer></script>

<!-- internal: a script element, usually just before the closing body tag -->
<script>
  console.log("Page loaded");
</script>

<!-- inline event attribute: works, but mixes code with HTML -->
<button onclick="showMessage()">Click me</button>

A script that uses page elements must run after those elements exist: put the script at the end of the body, use defer, or run the code in a load event handler. Otherwise getElementById returns null.

Comments, variables and assignment

// a single-line comment
/* a comment over
   several lines */
const VAT_RATE = 0.2;          // const: cannot be reassigned
let price = 40;                // let: can be reassigned
let total = price + price * VAT_RATE;
price = 50;                    // assignment changes the value
console.log(total);
console.log(price);

Output:

48
50

Data types and operators

Types: number (12, 4.5), string ("text" or 'text'), Boolean (true, false), arrays, objects, and undefined (declared but not given a value).

console.log(7 + 2, 7 - 2, 7 * 2, 7 / 2, 7 % 2);
console.log("7" + 2);          // + with a string joins (concatenates)
console.log("7" * 2);          // other arithmetic converts to number
console.log(Number("7") + 2);  // convert explicitly
let n = 5;
n += 3;                        // same as n = n + 3
n++;                           // add 1
console.log(n);
console.log(5 == "5", 5 === "5");

Output:

9 5 14 3.5 1
72
14
9
9
true false

% is the remainder (modulus). + adds numbers but concatenates if either value is a string, a very common bug with form input, because the value of an input is always a string: use Number() or parseInt() first. == compares after converting types; === (strict equality) also checks the type, so it is safer. Comparison operators: < > <= >= === !==. Logical operators: && (and), || (or), ! (not).

Worked example

A form box with id qty contains 3. The code `let q = document.getElementById("qty").value; console.log(q + 1);` displays 31, not 4. Explain and fix it.

  1. The value of an input is always a string: "3".
  2. + with a string concatenates: "3" + 1 is "31".
  3. Convert first: let q = Number(document.getElementById("qty").value); then q + 1 is 4.

Answer: Convert the input to a number with Number() or parseInt() before adding.

Key idea: Scripts go in an external file (best), a script element, or event attributes; run them after the elements exist. let can change, const cannot; = assigns; + joins strings; input values are strings, so convert them; use === for comparisons; comment your code.

Check yourself

  1. What is displayed when this code runs? ```js let a = "4"; let b = 2; console.log(a + b); ```
    Show answer

    42. a is a string, so + concatenates: 42.

  2. What is displayed when this code runs? ```js console.log(17 % 5); ```
    Show answer

    2. % gives the remainder: 17 = 3 x 5 + 2, so 2.

  3. What is displayed when this code runs? ```js let x = 10; let y = x; x = x + 5; console.log(y); ```
    Show answer

    10. y was given x's value (10) before x changed, so 10.

  4. What is displayed when this code runs? ```js console.log(3 === "3"); ```
    • false
    • true
    • 3
    • undefined
    Show answer

    false. === checks the type as well as the value; a number is not a string, so false.

  5. What is displayed when this code runs? ```js let score = 8; score += 4; score--; score *= 2; console.log(score); ```
    Show answer

    22. 8 + 4 = 12, minus 1 = 11, times 2 = 22.

  6. Which keyword declares a variable whose value must not be reassigned?
    • const
    • let
    • var
    • static
    Show answer

    const. const declares a constant; reassigning it causes a TypeError.

  7. A script in the head runs `document.getElementById("msg").textContent = "Hi";` but the console shows an error saying it cannot set properties of null. Why?
    • textContent cannot be changed by JavaScript
    • The id must be written with a #
    • The script runs before the element with id msg has been created
    • Scripts cannot go in the head
    Show answer

    The script runs before the element with id msg has been created. When the script runs, the body has not been read yet, so getElementById returns null. Use defer, move the script to the end of the body or use a load event.

  8. What is displayed when this code runs? ```js let input = "12"; console.log(Number(input) + 8); ```
    Show answer

    20. Number() converts "12" to 12, so 12 + 8 = 20.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.