Skip to content

Latest commit

 

History

History
82 lines (55 loc) · 3.85 KB

details.md

File metadata and controls

82 lines (55 loc) · 3.85 KB

Event loop

event loop is a fundamental concept in JavaScript that allows for asynchronous programming despite JavaScript being single-threaded. Here's how it works:

Call Stack: This is where your code is executed. Functions get pushed onto the stack when they're called and popped off when they return.

Web APIs: Browsers provide Web APIs for tasks like handling HTTP requests, timers (setTimeout, setInterval), and event listeners. When an asynchronous operation is initiated (e.g., a setTimeout or a fetch request), the function is offloaded to these APIs.

Callback Queue: Once an asynchronous operation is completed, its callback is placed in the callback queue (also known as the message queue).

Event Loop: The event loop continuously checks the call stack to see if it's empty. If the call stack is empty, it takes the first callback from the callback queue and pushes it onto the call stack for execution.

This process ensures that JavaScript can handle asynchronous tasks efficiently without blocking the execution of other code.

Prototypal Inheritance in JavaScript

In JavaScript, prototypal inheritance allows objects to inherit properties and methods from other objects. This is different from classical inheritance (as seen in languages like Java or C++) where classes inherit from other classes. Here’s a detailed explanation:

  1. Prototype Chain:

    • Every JavaScript object has a prototype, which is another object from which it inherits properties and methods.
    • When you create an object, JavaScript sets up a link between the new object and its prototype.
    • This forms a chain of prototypes, known as the prototype chain, which JavaScript follows to look up properties and methods.
  2. Constructor Functions and prototype:

    • You can create a constructor function to define an object type, and attach methods and properties to its prototype property. Objects created with this constructor will inherit these methods and properties.
    function Person(name) {
        this.name = name;
    }
    Person.prototype.greet = function() {
        console.log('Hello, ' + this.name);
    };
    const alice = new Person('Alice');
    alice.greet(); // Hello, Alice
    

Differences between var, let, and const

  • var:

    • Functional scope: Scoped to the function in which they are declared, or to the global scope if declared outside of any function.
    • Hoisted: Variables are moved to the top of their containing function or global scope.
    • Can be redeclared and updated.
  • let:

    • Block scope: Scoped to the block in which they are declared, such as a loop or an if statement.
    • Hoisted: Variables are moved to the top of their containing block.
    • Cannot be redeclared within the same block.
    • Can be updated.
  • const:

    • Block scope: Scoped to the block in which they are declared.
    • Hoisted: Variables are moved to the top of their containing block.
    • Must be initialized with a value at the time of declaration.
    • Cannot be reassigned after initialization.

Synchronous Programming

In synchronous programming, tasks are executed sequentially, one after the other. Each task must wait for the previous one to complete before it can start. This means that if a task takes a long time to complete, it can block the execution of subsequent tasks, causing the program to become unresponsive.

Example:

// Synchronous code
console.log('Task 1');
console.log('Task 2');
console.log('Task 3');



### Asynchronous Programming

In asynchronous programming, tasks are executed concurrently, and the program does not wait for one task to complete before moving on to the next. Instead, asynchronous tasks are executed in the background, and the program continues to run without being blocked.

// Asynchronous code
console.log('Task 1');
setTimeout(() => {
    console.log('Task 2 (after 2 seconds)');
}, 2000);
console.log('Task 3');