Purpose And Data Types
JavaScript adds dynamic functionality like form validation or interactive maps
- Integer - whole numbers. Example:
let count = 5; - Real - decimal numbers. Example:
let price = 10.99; - String - text. Example:
let name = "Alice"; - Boolean - true or false. Example:
let isFound = false;
Variables And Constants
- Variables (
let) - can be changed during execution. Example:let score = 0; score = 10; - Constants (
const) - remain the same. Example:const TAX_RATE = 0.2;
Arithmetic Operators
Used for mathematical calculations
- Basic -
+,-,*,/ - Modulus (
%) - returns remainder. Example:10 % 3is1 - Exponentiation (
**) - power of. Example:2 ** 3is8 - Increment/Decrement -
++,--. Example:x++adds 1 to x
Comparison And Logical Operators
- Equal Value (
==) - Example:5 == "5"istrue - Strict Equal (
===) - checks value and type. Example:5 === "5"isfalse - Logic -
&&(AND),||(OR),!(NOT). Example:(x > 0 && x < 10)
Strings
- Length -
.length. Example:"Hello".lengthis5 - Substring -
.substring(start, end). Example:"Apple".substring(0, 3)is"App"
Selection And Decision Making
- If Statements - choose code to run if a condition is true
if (age >= 18) { console.log("Adult"); } else { console.log("Minor"); }- Switch Case - useful for comparing a single expression against multiple possible values
- break - prevents “fall-through” (executing the next case automatically)
- default - runs if no matches are found Example:
switch (grade) {
case 'A': console.log("Great"); break;
case 'B': console.log("Good"); break;
default: console.log("Keep trying");
}Iteration And Loops
- For Loops - used when the number of iterations is known
- Standard -
for (initialisation; condition; increment) - Example:
for (let i = 0; i < 5; i++) { console.log(i); } - For…in - iterates over the indices of an array
- Example:
for (let x in fruits) { console.log(fruits[x]); } - While And Do While Loops - used when the number of iterations is unknown in advance
- while - checks the condition before running the code. (May run 0 times)
- Example:
while (password != "secret") { password = prompt("Enter pass"); } - do while - checks the condition after running the code. (Always runs at least once)
- Example:
do { roll = Math.random(); } while (roll < 0.5);
Nested Statements
You can place one construct inside another (e.g., an if inside a for loop)
- Tip - to find the total iterations of a nested loop, multiply the outer loop count by the inner loop count
- Example:
for (let i = 0; i < 3; i++) { // Outer
for (let j = 0; j < 5; j++) { // Inner runs 15 times total (3 * 5)
console.log(i, j);
}
}Functions And Procedures
Blocks of code that can be reused throughout a programme
- Function - performs a task and returns a value using the
returnkeyword - Example:
function add(a, b) { return a + b; } - Procedure - performs a task but does not return a value
- Example:
function greet() { console.log("Hello!"); }
Producing Output
innerHTML- modifies content within an element- Example:
document.getElementById("msg").innerHTML = "Hello"; document.write()- writes directly to the document- Example:
document.write("Goodbye!"); alert()- displays a pop-up box- Example:
alert("Warning!");
Examiner Tip
If you are asked to complete a JavaScript function in the exam, always check if you need to use the
returnkeyword to pass the result back to the programme!