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

Subprograms (functions) and object orientation in JavaScript

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

Functions break code into reusable named blocks that take parameters and return values; objects group data and behaviour together (spec 9.3.2 h and i).

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

Practise this lesson

Functions, parameters and arguments

function area(width, height) {     // width and height are parameters
  return width * height;
}
function greet(name) {             // a function with no return value
  console.log("Hello " + name);
}
let a = area(4, 5);                // 4 and 5 are arguments
console.log(a);
greet("Sam");
console.log(area(2, 3) + area(1, 1));

Output:

20
Hello Sam
7

Scope and function expressions

let count = 0;                       // global: visible everywhere
function addOne() {
  let step = 1;                      // local: only inside the function
  count = count + step;
}
addOne();
addOne();
console.log(count);
const double = function (n) { return n * 2; };
const triple = n => n * 3;           // arrow function
console.log(double(5), triple(5));

Output:

2
10 15

A variable declared with let or const inside a function (or any block in curly brackets) is local to it. Use local variables where possible, so functions do not interfere with each other. Functions can be stored in variables and passed to other functions, as event handlers are.

Objects and classes (object orientation)

const member = {
  name: "Ravi",
  visits: 3,
  addVisit: function () { this.visits++; }
};
member.addVisit();
console.log(member.name + " " + member.visits);

class Booking {
  constructor(name, people) {
    this.name = name;
    this.people = people;
  }
  cost(pricePerPerson) {
    return this.people * pricePerPerson;
  }
}
const b1 = new Booking("Lee", 4);
console.log(b1.name + " pays " + b1.cost(7.5));

Output:

Ravi 4
Lee pays 30

An object has properties (data, such as name) and methods (functions, such as addVisit). this refers to the object the method belongs to. A class is a template; new creates an instance (an object) and runs the constructor. The DOM itself is made of objects: document is an object, and every element is an object with properties (textContent, style) and methods (addEventListener).

Worked example

Write a function calcTotal that takes a price and a quantity and returns the total with 20% VAT added. Call it for a price of 5 and a quantity of 3.

  1. Two parameters: price and quantity.
  2. Return price * quantity * 1.2.
  3. Call it with the arguments 5 and 3: 18

Answer: ```js function calcTotal(price, quantity) { return price * quantity * 1.2; } console.log(calcTotal(5, 3)); ```

Key idea: function name(parameters) { ... return value; } is called with arguments. Local variables exist only inside their function. Objects hold properties and methods; a class is a template, new makes an instance, this means the current object.

Check yourself

  1. What is displayed when this code runs? ```js function f(x) { return x * x + 1; } console.log(f(3) + f(2)); ```
    Show answer

    15. f(3) = 10 and f(2) = 5, so 15.

  2. In `function tax(amount, rate) { return amount * rate; }` and the call `tax(200, 0.2)`, what are 200 and 0.2?
    • Parameters
    • Arguments
    • Return values
    • Properties
    Show answer

    Arguments. Values passed in a call are arguments; amount and rate in the definition are parameters.

  3. What is displayed when this code runs? ```js function add(a, b) { let s = a + b; } console.log(add(2, 3)); ```
    • 5
    • s
    • 23
    • undefined
    Show answer

    undefined. add has no return statement, so it gives undefined.

  4. What is displayed when this code runs? ```js let total = 10; function change() { let total = 99; total = total + 1; } change(); console.log(total); ```
    Show answer

    10. The let inside change() makes a separate local variable; the global total is unchanged: 10.

  5. What is displayed when this code runs? ```js const counter = { value: 5, inc: function (n) { this.value += n; } }; counter.inc(3); counter.inc(2); console.log(counter.value); ```
    Show answer

    10. 5 + 3 + 2 = 10; this refers to counter.

  6. What is displayed when this code runs? ```js class Rect { constructor(w, h) { this.w = w; this.h = h; } perimeter() { return 2 * (this.w + this.h); } } const r = new Rect(3, 4); console.log(r.perimeter()); ```
    Show answer

    14. 2 x (3 + 4) = 14.

  7. Give one benefit of writing code as functions rather than one long block.
    Show answer

    The function can be reused by calling it several times, so the code is not repeated.. Functions avoid repetition (write once, call many times), are easier to test and debug, and make the program easier to read and maintain.

Practise with instant feedback

Sources

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