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
Object Oriented Programming Articles
Page 91 of 589
How to terminate javascript forEach()?
You can't break from the forEach method and it doesn't provide a way to escape the loop (other than throwing an exception). However, there are several alternative approaches to achieve early termination when iterating over arrays. The Problem with forEach The forEach() method is designed to iterate through all array elements. Unlike traditional loops, it doesn't support break or continue statements: // This will NOT work - break is not allowed in forEach [1, 2, 3, 4].forEach((element) => { console.log(element); if (element === 2) { ...
Read MoreWhat is the use of sinon.js?
SinonJS provides standalone test spies, stubs and mocks for JavaScript unit testing. It helps create fake objects and functions to isolate and test code in controlled environments. What is SinonJS? SinonJS is a testing library that provides utilities for creating test doubles. These help you test code in isolation by replacing dependencies with controlled fake implementations. Core Features SinonJS offers three main types of test doubles: Spies — Track function calls without changing behavior. They record how functions are called. Stubs — Replace functions with controlled implementations. You can define what they return or ...
Read MoreWhat 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 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 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 MoreHow can you detect the version of a browser using Javascript?
In this article, we are going to discuss how to detect the version of a browser using JavaScript. These days, most browsers are JavaScript-enabled. But still, there are some browsers that don't support JavaScript; or, some versions of some browsers don't support certain features of JavaScript. Therefore, in certain instances, there is a need to know the details of the client's web browser so that the applications can deliver appropriate content. Detecting the Version of a Browser You can detect the details of the current browser using navigator.appName and navigator.appVersion properties. To detect the version ...
Read More