Articles on Trending Technologies

Technical articles with clear explanations and examples

Get max value per key in a JavaScript array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 522 Views

When working with arrays of objects in JavaScript, you might need to find the object with the maximum value for a specific property within each group. This is a common task when analyzing data grouped by categories. Consider this array of fruit data: const arr = [ {a:1, b:"apples"}, {a:3, b:"apples"}, {a:4, b:"apples"}, {a:1, b:"bananas"}, {a:3, b:"bananas"}, {a:5, b:"bananas"}, {a:6, b:"bananas"}, {a:3, b:"oranges"}, ...

Read More

How to test if a parameter is provided to a function in JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 4K+ Views

Testing if a parameter is provided to a function in JavaScript is an important part of writing effective code. It allows you to ensure that the arguments passed into your functions are valid and properly formatted for use within the function. In this article, we will discuss different methods for testing whether or not a parameter has been provided to a function in JavaScript. Let us understand the term "Parameter". A parameter is a named entity in a function definition that specifies an argument which the function can accept. It acts as a variable within the function and its ...

Read More

Trigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 341 Views

By default, the click event fires when you press and then release the mouse button. To trigger an event immediately when the mouse button is pressed down, use the mousedown event instead. The Difference Between click and mousedown The click event requires a complete click cycle (press down + release), while mousedown fires immediately when the button is pressed. Using mousedown Event Here's how to trigger an event immediately on mouse press: Mousedown Event Example ...

Read More

Join in nested array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 540 Views

Joining elements from nested arrays in JavaScript requires flattening the array structure first. This article explores different approaches to join nested array elements with a semicolon separator. The Problem Consider this nested array: const arr = ['zero', ['one', 'two', 'three', ['four', ['five', 'six', ['seven']]]]]; console.log("Original nested array:", arr); Original nested array: [ 'zero', [ 'one', 'two', 'three', [ 'four', [Array] ] ] ] We need to extract all elements and join them with semicolons to get: zero;one;two;three;four;five;six;seven; Using flat() Method (Modern Approach) The flat(Infinity) method flattens ...

Read More

Preparing encoding and decoding algorithms for shortening URLs in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 924 Views

URL shortening services like bit.ly and TinyURL take long URLs and convert them into shorter, more manageable links. This process involves encoding the original URL into a compact format and later decoding it back when accessed. To implement a basic URL shortening system in JavaScript, we need two main functions: encrypt() → takes the original URL and returns a shortened unique URL decrypt() → takes the shortened URL and converts it back to the original URL Basic Implementation Using Base64 Encoding Here's a simple approach using ...

Read More

Rearranging digits to form the greatest number using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 774 Views

Problem We are required to write a JavaScript function that takes in one positive three-digit integer and rearranges its digits to get the maximum possible number. Approach To create the maximum number, we need to arrange the digits in descending order. We can convert the number to a string, split it into individual digits, sort them in descending order, and join them back. Example Following is the code − const num = 149; const maxRedigit = function(num) { if(num < 100 || num > 999) ...

Read More

Is the digit divisible by the previous digit of the number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

Problem We need to write a JavaScript function that takes a number and checks if each digit is divisible by the digit on its left. The function returns an array of booleans, where the first element is always false since there's no digit before the first one. Example Let's check the number 73312 digit by digit: const num = 73312; const divisibleByPrevious = (n = 1) => { const str = n.toString(); const arr = [false]; for(let i = 1; ...

Read More

How to wait resize end event and then perform an action using JavaScript?

Shubham Vora
Shubham Vora
Updated on 15-Mar-2026 3K+ Views

When resizing a browser window, the 'resize' event fires continuously during the resize operation. This can cause performance issues if you execute heavy JavaScript code on every resize event. The solution is to wait until the resize operation ends before performing any action. The most common approach is using setTimeout() and clearTimeout() to create a debounced resize handler that only executes after the user stops resizing. Syntax let timeoutId; window.addEventListener('resize', () => { // Clear the previous timeout clearTimeout(timeoutId); // Set a ...

Read More

How to join JavaScript array of string

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 190 Views

We need to write a JavaScript function that joins an array of strings, replaces all whitespaces with dashes "-", and returns the formatted string. For example: If the array is − const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; Then the output should be − const output = "QA-testing-promotion-Twitter-Facebook-Test"; Method 1: Using Loop and String Concatenation This approach joins the array elements and processes each character to replace spaces with dashes: const arr = ["QA testing promotion ", " Twitter ", "Facebook ", ...

Read More

Finding closest pair sum of numbers to a given number in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 321 Views

We need to write a JavaScript function that takes an array of numbers and a target number, then returns the pair of numbers from the array whose sum is closest to the target. The function should find two different numbers from the original array that, when added together, produce a sum nearest to the specified target value. Basic Approach Using Nested Loops Here's a straightforward solution using nested loops to check all possible pairs: const arr = [1, 2, 3, 4, 5, 6, 7]; const num = 14; const closestPair = (arr, target) => ...

Read More
Showing 16231–16240 of 61,297 articles
Advertisements