2026-10-05
Example:
String, Number, Boolean, Array, Object
Example:
Example:
Example:
Example:
Example:
Example:
Example:
JavaScript uses prototypal inheritance, where objects inherit properties and methods from a prototype object.
Example:
ES6 introduced class syntax to simplify prototypal inheritance.
Example:
class Product {
constructor(name, price) {
this.name = name;
this.price = price;
}
display() {
console.log(`${this.name} costs $${this.price}`);
}
}
class Electronic extends Product {
constructor(name, price, brand) {
super(name, price);
this.brand = brand;
}
display() {
console.log(`${this.brand} ${this.name} costs $${this.price}`);
}
}
let laptop = new Electronic("Laptop", 999.99, "BrandX");
laptop.display(); // BrandX Laptop costs $999.99Example:
Example:
Example:
class Product {
name: string;
price: number;
constructor(name: string, price: number) {
this.name = name;
this.price = price;
}
display(): void {
console.log(`${this.name} costs $${this.price}`);
}
}
class Electronic extends Product {
brand: string;
constructor(name: string, price: number, brand: string) {
super(name, price);
this.brand = brand;
}
display(): void {
console.log(`${this.brand} ${this.name} costs $${this.price}`);
}
}
let laptop = new Electronic("Laptop", 999.99, "BrandX");
laptop.display(); // BrandX Laptop costs $999.99Example:
<ul id="productList"></ul>
<script>
let products = ["Laptop", "Smartphone", "Tablet"];
let productList = document.getElementById("productList");
products.forEach(product => {
let listItem = document.createElement("li");
listItem.textContent = product;
productList.appendChild(listItem);
});
</script>