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).
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- A variable is a named store for a value. Declare it with let (value can change) or const (a constant that cannot be reassigned). Older code uses var.
- = is assignment: it stores the value on the right in the variable on the left. total was calculated when price was 40, so changing price later does not change total.
- Names are case-sensitive (total and Total are different); use meaningful camelCase names such as basketTotal.
- Comments explain the code. Pearson's mark schemes give marks for comments, meaningful names and indentation in the coding questions.
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.
- The value of an input is always a string: "3".
- + with a string concatenates: "3" + 1 is "31".
- 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.
Check yourself
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Information Technology specification (first teaching 2018), Unit 2 Topics 7 to 11
- Pearson qualifications: Edexcel International Advanced Level Information Technology (2018)
- Pearson WIT12/01 mark scheme, June 2024 series
Last updated 8 November 2026. Spotted a mistake? Tell us.