Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Ayush Gupta
433 articles
What are the differences between Deferreds, Promises and Futures in javascript?
In JavaScript asynchronous programming, the terms Deferred, Promise, and Future are often used interchangeably, but they have subtle differences in their origins and implementations. What is a Future? Future is an older term from other programming languages that represents the same concept as a JavaScript Promise. In modern JavaScript, "Future" and "Promise" refer to the same thing - a placeholder for a value that will be available later. What is a Promise? A Promise represents a value that is not yet known. It acts as a proxy for a value that may not be available when ...
Read MoreWhat is the difference between 'throw new Error' and 'throw someObject' in javascript?
In JavaScript, both throw new Error and throw someObject can be used to throw exceptions, but they differ in structure and best practices. Using throw new Error When you use throw new Error(), JavaScript creates a proper Error object with standard properties: try { throw new Error("Something went wrong"); } catch (error) { console.log("Name:", error.name); console.log("Message:", error.message); console.log("Stack:", error.stack ? "Available" : "Not available"); } Name: Error Message: Something went wrong Stack: Available Using throw ...
Read MoreIf a DOM Element is removed, are its listeners also removed from memory in javascript?
In modern browsers, when a DOM element is removed from the document, its event listeners are automatically removed from memory through garbage collection. However, this only happens if the element becomes completely unreferenced. How Garbage Collection Works with DOM Elements Event listeners are automatically cleaned up when: The DOM element is removed from the document No JavaScript variables hold references to the element The element becomes eligible for garbage collection Example: Automatic Cleanup Click Me Remove Button ...
Read MoreJasmine JavaScript Testing - toBe vs toEqual
In Jasmine JavaScript testing, toBe and toEqual are two different matchers for comparing values. Understanding their difference is crucial for writing effective tests. toBe vs toEqual Overview Arrays and objects can be compared in two ways: Reference equality: They refer to the same object in memory Value equality: They may refer to different objects but their contents are identical Using toBe (Reference Equality) The toBe matcher checks if two variables reference the exact same object in memory. It uses JavaScript's === operator internally. ...
Read MoreWhat blocks Ruby, Python to get Javascript V8 speed?
Nothing technically prevents Ruby and Python from achieving JavaScript V8 speeds. The performance gap exists primarily due to differences in optimization investments and language design choices rather than fundamental limitations. The V8 Advantage Google's V8 engine benefits from massive engineering resources and specific optimizations: Just-In-Time (JIT) compilation: Converts JavaScript to optimized machine code at runtime Hidden class optimization: Optimizes object property access patterns Inline caching: Speeds up method calls and property lookups Garbage collection tuning: Minimizes pause times during memory cleanup Ruby and Python Constraints Several factors limit Ruby and Python performance compared ...
Read MoreHow to get a subset of a javascript object's properties?
To get a subset of an object's properties and create a new object with selected properties, you can use object destructuring combined with property shorthand. This technique allows you to extract specific properties and create a new object containing only those properties. Basic Example Let's start with an object containing multiple properties: const person = { name: 'John', age: 40, city: 'LA', school: 'High School' }; // Extract only name and age const {name, age} = person; const selectedObj ...
Read MoreWhat Is a JavaScript Framework?
JavaScript frameworks are pre-built code libraries that provide structured solutions for common programming tasks and features. They serve as a foundation to build websites and web applications more efficiently than using plain JavaScript. Instead of manually writing repetitive code, frameworks offer ready-made components, utilities, and architectural patterns that speed up development and enforce best practices. Why Use JavaScript Frameworks? Plain JavaScript requires manual DOM manipulation for every interaction: ...
Read MoreHow to test if a letter in a string is uppercase or lowercase using javascript?
To test if a letter in a string is uppercase or lowercase using JavaScript, you can convert the character to its respective case and compare the result. This method works by checking if the character remains the same after case conversion. Basic Approach The most straightforward method is to compare a character with its uppercase and lowercase versions: function checkCase(ch) { if (!isNaN(ch * 1)) { return 'ch is numeric'; } else { ...
Read MoreGet the first and last item in an array using JavaScript?
JavaScript arrays are 0-indexed, meaning the first element is at position 0 and the last element is at position length - 1. Here are several ways to access the first and last elements of an array. Using Index Access The most straightforward method uses bracket notation with index positions: let arr = [1, 'test', {}, 'hello', 42]; // First element console.log("First element:", arr[0]); // Last element console.log("Last element:", arr[arr.length - 1]); First element: 1 Last element: 42 Using Array Methods JavaScript provides built-in methods for accessing ...
Read MoreWhy doesn't JavaScript support multithreading?
JavaScript is fundamentally single-threaded by design, executing code through an event loop mechanism. This architectural choice was made for specific reasons related to web development and browser safety. The Single-Threaded Nature JavaScript runs on a single main thread, processing one operation at a time. The Event Loop has one simple job — to monitor the Call Stack and the Callback Queue. If the Call Stack is empty, it will take the first event from the queue and push it to the Call Stack, which effectively runs it. console.log("First"); setTimeout(() => { ...
Read More