Javascript Articles

Page 418 of 534

How can I trigger an onchange event manually in javascript?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 28K+ Views

In JavaScript, you can manually trigger an onchange event using the dispatchEvent() method with a custom Event object. This is useful for programmatically simulating user interactions. Basic Setup First, let's create an input element with a change event listener: document.querySelector('#test').addEventListener('change', () => { console.log("Changed!"); }); Method 1: Using Event Constructor Create a new Event object and dispatch it on the target element: Trigger Change document.querySelector('#example1').addEventListener('change', () => { console.log("Change event fired!"); }); function triggerChange1() { ...

Read More

How to terminate javascript forEach()?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 574 Views

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 More

What is the use of sinon.js?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 600 Views

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 More

What are the differences between Deferreds, Promises and Futures in javascript?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 1K+ Views

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 More

What is the difference between 'throw new Error' and 'throw someObject' in javascript?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 599 Views

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 More

If a DOM Element is removed, are its listeners also removed from memory in javascript?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 3K+ Views

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 More

How to get a subset of a javascript object's properties?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 368 Views

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 More

What Is a JavaScript Framework?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 1K+ Views

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 More

How to test if a letter in a string is uppercase or lowercase using javascript?

Ayush Gupta
Ayush Gupta
Updated on 15-Mar-2026 8K+ Views

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 More

How to deserialize a JSON into Javascript object?

Aman Kumar
Aman Kumar
Updated on 15-Mar-2026 3K+ Views

Serialization is the process of converting an object such that it is transferable over the network. In JavaScript usually we serialize an Object into the JSON (JavaScript Object Notation) format. To deserialize a JSON string back into a JavaScript object, we use the JSON.parse() method. This method takes a JSON string and converts it into a JavaScript object or array that we can work with in our code. JavaScript object notation is commonly used to exchange data with web servers and REST APIs. The data we receive from a web server is always a string. To use this ...

Read More
Showing 4171–4180 of 5,340 articles
« Prev 1 416 417 418 419 420 534 Next »
Advertisements