JavaScript is a high-level, dynamic, and interpreted programming language that is commonly used to create interactive effects within web browsers. It is a client-side scripting language, meaning it runs in the web browser, allowing developers to create interactive web pages, handle user events, and manipulate the DOM (Document Object Model) in real-time. JavaScript is an essential technology for web development, alongside HTML and CSS, forming the backbone of web application interactivity.
variable and Variable are treated as different identifiers.;), though semicolons are optional in most cases.JavaScript has several primitive data types and objects that help you store and manipulate data. The primary data types include:
let age = 25; // integer
let price = 19.99; // floating point
let name = "Alice"; // string
true or false.
let isActive = true; // boolean
undefined.
let car; // undefined
let user = null; // null
let sym = Symbol('unique');
2^53 - 1, introduced in ES11 (ECMAScript 2020).
let bigNum = 123456789123456789123456789n; // BigInt
let person = {
name: "John",
age: 30,
city: "New York"
};
let colors = ["red", "green", "blue"];
In JavaScript, variables are used to store values. There are three ways to declare variables:
var: This is the traditional way to declare variables, but it has some limitations (such as function scoping). It’s considered less commonly used in modern JavaScript.
var name = "Alice";
let: Declares a block-scoped variable (introduced in ES6) that can be reassigned. It’s preferred over var in most cases.
let age = 25;
age = 26; // reassignment is allowed
const: Declares a block-scoped variable that cannot be reassigned (it’s constant). It must be assigned a value when declared.
const pi = 3.14159; // cannot be reassigned
A function in JavaScript is a block of code designed to perform a specific task. Functions can accept inputs (parameters) and return outputs (return values). Functions are one of the building blocks of JavaScript, allowing for code reusability and modularity.
You can define a function using the function keyword.
function greet(name) {
return "Hello, " + name + "!";
}
Once a function is defined, you can call (or invoke) it by using the function name and providing any required arguments.
let message = greet("Alice"); // calling the function
console.log(message); // Output: Hello, Alice!
Example:
function add(a, b) {
return a + b;
}
let result = add(5, 3); // result will be 8
console.log(result); // Output: 8
An anonymous function is a function without a name. It can be assigned to variables or passed as arguments to other functions.
Example of an anonymous function assigned to a variable:
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(4, 3)); // Output: 12
Arrow functions provide a more concise way to write functions. They also have a different behavior for this.
const subtract = (a, b) => a - b;
console.log(subtract(10, 4)); // Output: 6
JavaScript also provides several control structures to handle decision-making and looping:
If-Else Statement: Used to execute code based on conditions.
let number = 10;
if (number > 5) {
console.log("Greater than 5");
} else {
console.log("Less than or equal to 5");
}
Switch Statement: A more readable alternative to multiple if-else conditions.
let day = "Monday";
switch(day) {
case "Monday":
console.log("Start of the week");
break;
case "Friday":
console.log("End of the week");
break;
default:
console.log("Midweek");
}
Loops: JavaScript offers several looping mechanisms such as for, while, and forEach.
For loop:
for (let i = 0; i < 5; i++) {
console.log(i); // Output: 0, 1, 2, 3, 4
}
While loop:
let j = 0;
while (j < 5) {
console.log(j); // Output: 0, 1, 2, 3, 4
j++;
}
JavaScript is a powerful and versatile language, enabling developers to create dynamic, interactive web pages. Understanding its fundamental concepts such as data types, variables, and functions is key to mastering the language. By leveraging these tools, you can develop a wide variety of web applications, from simple dynamic effects to full-fledged web applications.
Open this section to load past papers