Posts

Classes and Objects in Javascript | Lecture 10

  In JavaScript, classes are templates for creating objects, and objects are instances of classes. Classes encapsulate data and methods to model real-world entities. Declaring a Class A class is defined using the class keyword, and it typically contains: Constructor : A special method for initializing the object. Methods : Functions defined within the class. Example: Creating a Class and an Object class Person {   // Constructor to initialize the object   constructor(name, age) {     this.name = name; // Instance property     this.age = age;   }     // Method   greet() {     return `Hello, my name is ${this.name} and I am ${this.age} years old.`;   } }   // Create an object (instance) of the class const person1 = new Person("John", 30);   // Access properties and methods console.log(person1.name); // Output: John console.log...

JavaScript Promises | Lecture 9

  JavaScript Promises are a way to handle asynchronous operations. A promise represents a value that might be available now, in the future, or never. They are used to avoid callback hell and make asynchronous code easier to read and manage. Key States of a Promise: Pending : The promise is still pending and hasn't been resolved or rejected. Fulfilled : The promise was completed successfully. Rejected : The promise failed. Basic Syntax const promise = new Promise((resolve, reject) => {   // asynchronous operation   if (successCondition) {     resolve("Success!"); // When operation succeeds   } else {     reject("Error!"); // When operation fails   } }); Handling Promises then() : Executes when the promise is resolved. catch() : Executes when the promise is rejected. finally() : Executes after the promise is settled (either resolved or rejected). Examples E...