Maths
Maths
Introduction to JavaScript
JavaScript is a high-level, dynamically typed programming language widely used for web
development. It enables interactive web pages and is an essential technology alongside HTML and
CSS. JavaScript can be executed in web browsers and server-side environments like Node.js.
Setting Up JavaScript
To write JavaScript, you can use:
// Declaring variables
var globalVar = "I am global"; // Function-scoped
let blockVar = "I am block-scoped"; // Block-scoped
const constantVar = "Cannot change"; // Constant value
// Arithmetic Operators
let sum = 10 + 5;
let product = 10 * 5;
// Comparison Operators
console.log(5 == "5"); // true (loose equality)
console.log(5 === "5"); // false (strict equality)
// Logical Operators
console.log(true && false); // false
console.log(true || false); // true
console.log(!true); // false
// If-Else Statement
let age = 20;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
// Ternary Operator
let message = (age >= 18) ? "Adult" : "Minor";
console.log(message);
// Switch Case
let fruit = "banana";
switch (fruit) {
case "apple":
console.log("It's an apple!");
break;
case "banana":
console.log("It's a banana!");
break;
default:
console.log("Unknown fruit");
}
// For Loop
for (let i = 0; i < 5; i++) {
console.log("Iteration:", i);
}
// While Loop
let count = 0;
while (count < 3) {
console.log("Count:", count);
count++;
}
// Do-While Loop
let num = 0;
do {
console.log("Number:", num);
num++;
} while (num < 3);
// Function Declaration
function greet(name) {
return "Hello, " + name;
}
console.log(greet("Alice"));
// Function Expression
const add = function(a, b) {
return a + b;
};
console.log(add(4, 5));
// Arrow Function
const multiply = (a, b) => a * b;
console.log(multiply(3, 5));
// Object Creation
let person = {
name: "Alice",
age: 22,
greet: function() {
return "Hello, " + this.name;
}
};
console.log(person.greet());
// Adding Properties
person.city = "New York";
console.log(person.city);
// Prototype Example
function Car(model, year) {
this.model = model;
this.year = year;
}
Car.prototype.getCarInfo = function() {
return `${this.model}, ${this.year}`;
};
Asynchronous JavaScript
// setTimeout Example
setTimeout(() => console.log("Executed after 2 seconds"), 2000);
// Async-Await Example
async function fetchData() {
let response = await fetch("https://jsonplaceholder.typicode.com/users");
let users = await response.json();
console.log(users);
}
fetchData();
// Closure Example
function outerFunction(outerVar) {
return function innerFunction(innerVar) {
return `Outer: ${outerVar}, Inner: ${innerVar}`;
};
}
// Selecting an Element
let button = document.getElementById("btn");
// Singleton Pattern
const Singleton = (function() {
let instance;
function createInstance() {
return { message: "I am the only instance" };
}
return {
getInstance: function() {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
console.log(Singleton.getInstance());
// Observer Pattern
class Subject {
constructor() {
this.observers = [];
}
subscribe(observer) {
this.observers.push(observer);
}
notify(data) {
this.observers.forEach(observer => observer.update(data));
}
}
class Observer {
update(data) {
console.log("Observer received:", data);
}
}