Posts

SOLID / DRY principles

In software development, Object-Oriented Design plays a crucial role when it comes to writing flexible, scalable, maintainable, and reusable code. There are so many benefits of using OOD but every developer should also have the knowledge of the SOLID principle for good object-oriented design in programming. The SOLID principles are designed to help developers design robust, maintainable applications. The five SOLID principles are: Single-responsibility principle - Function and classes should have one job. Open–closed principle - Classes and functions should be open for  extension but not for modification. Liskov substitution principle - Child class can replace parent class Interface segregation principle - This principle is the first principle that applies to Interfaces instead of classes in SOLID and it is similar to the single responsibility principle. It states that “do not force any client to implement an interface which is irrelevant to them“. Here your main goal is ...

Why do we need callbacks

In JavaScript, callbacks are commonly used due to the language's asynchronous nature and event-driven programming model. Here are some specific reasons why callbacks are important in JavaScript: 1. Asynchronous Operations: JavaScript often handles operations that take time to complete, such as making AJAX requests, fetching data from APIs, or reading files. Callbacks allow you to specify what should happen once these operations are finished. By passing a callback function as an argument, you can define the behavior that should occur once the asynchronous operation completes, ensuring that the code doesn't block while waiting for a response. 2. Event Handling: JavaScript is extensively used for building interactive web applications that respond to user actions, such as button clicks, form submissions, or mouse movements. Callbacks are employed to handle these events by attaching callback functions to event listeners. When an event occurs, the associated callback function is exec...

Callback function

A callback function is a function that is passed as an argument to another function and is executed by that function at a specific time or in response to a certain event. The callback function is typically used to customize or define additional behavior for the calling function. In JavaScript, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned as values from functions. This flexibility allows callback functions to be used extensively in JavaScript for handling asynchronous operations, event handling, and functional programming. Here are the key characteristics of a callback function: 1. Passed as an Argument: The callback function is passed as an argument to another function. It can be defined inline or as a separate named function. 2. Execution by the Calling Function: The calling function is responsible for executing the callback function at an appropriate time. This could be after completing an asynchro...

Difference between localStorage and sessionStorage

Both `localStorage` and `sessionStorage` are web storage options provided by modern web browsers to store data on the client-side. They have some similarities but also important differences in terms of data persistence and scope. Here's a breakdown of the key differences: 1. Data Persistence:    - `localStorage`: The data stored in `localStorage` persists even after the browser window is closed and reopened. It remains available until explicitly cleared or removed.    - `sessionStorage`: The data stored in `sessionStorage` persists only for the duration of the current browser session. When the browser window or tab is closed, the data is cleared and no longer accessible. 2. Scope:    - `localStorage`: The data stored in `localStorage` is accessible across multiple windows or tabs of the same origin. This means that data set in one window/tab can be accessed in another window/tab of the same origin.    - `sessionStorage`: The data stored in `sessio...

Scope in javascript

In JavaScript, scope refers to the visibility and accessibility of variables, functions, and objects in a particular part of your code. It determines where and how variables can be accessed and modified. There are two main types of scope in JavaScript: global scope and local scope. 1. Global Scope:    - Variables declared outside of any function have global scope.    - Global variables can be accessed from anywhere in your code, including inside functions.    - Example:    var globalVariable = "I'm a global variable";    function printGlobal() {      console.log(globalVariable);    }    printGlobal(); // Output: I'm a global variable 2. Local Scope:    - Variables declared within a function have local scope, meaning they are only accessible within that function (including any nested functions).    - Local variables take precedence over global variables with the same name.   ...

What is memoization

Memoization is a technique used in JavaScript (and other programming languages) to optimize the performance of functions by caching the results of expensive function calls. It involves storing the computed values of function calls and returning the cached result when the same inputs are provided again. The idea behind memoization is that if a function is called with the same arguments multiple times, instead of recomputing the result every time, the function can return the previously computed result from a cache. This can significantly improve the execution time, especially for functions that involve complex or time-consuming computations. Here's an example of a memoized function in JavaScript: function memoizedFunction(n) {   if (memoizedFunction.cache[n] !== undefined) {     console.log("Fetching from cache");     return memoizedFunction.cache[n];   } else {     console.log("Computing result");     const result = n * 2;     m...

IIFE(Immediately Invoked Function Expression)

  IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The signature of it would be as below, ( function ( ) { // logic here } ) ( ) ; The primary reason to use an IIFE is to obtain data privacy because any variables declared within the IIFE cannot be accessed by the outside world. i.e, If you try to access variables with IIFE then it throws an error as below, ( function ( ) { var message = "IIFE" ; console . log ( message ) ; } ) ( ) ; console . log ( message ) ; //Error: message is not defined

Popular posts from this blog

Difference between localStorage and sessionStorage

Scope in javascript

What is memoization