Articles on Trending Technologies

Technical articles with clear explanations and examples

How to copy text to the clipboard with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 678 Views

To copy text to the clipboard in JavaScript, we can use both modern and legacy approaches. The modern navigator.clipboard API is recommended, while the older document.execCommand() method provides fallback support. Modern Approach: Using navigator.clipboard API The navigator.clipboard.writeText() method is the modern standard for copying text. It returns a Promise and works asynchronously. button { border: none; ...

Read More

Explain shorthand functions in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Arrow functions, also known as shorthand functions, were introduced in ES2015 and allow us to write functions in a shorter, more concise way. They don't have their own binding to this and inherit this from the surrounding context. Basic Syntax Arrow functions use the => syntax instead of the function keyword: // Regular function function add(a, b) { return a + b; } // Arrow function const add = (a, b) => a + b; Different Arrow Function Forms ...

Read More

Algorithm to dynamically populate JavaScript array with zeros before and after values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

We are given a months array, which contains elements less than 12, where each element will be between 1 and 12 (both inclusive). Our job is to take this array and create a full months array with 12 elements. If the element is present in the original array we use that element, otherwise we use 0 at that place. For example: Input → [5, 7, 9] Output → [0, 0, 0, 0, 5, 0, 7, 0, 9, 0, 0, 0] Now, let's write the code: Using Array.includes() Method const months = [6, ...

Read More

Group array by equal values JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Let's say, we have an array of string/number literals that contains some duplicate values like this: const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night']; We are required to write a function groupSimilar() that takes in this array and returns a new array where all the repeating entries are grouped together in a subarray as the first element and their total count in the original array as the second element. So, for this example, the output should be: [ [ 'day', 3 ], ...

Read More

Recursive multiplication in array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

We need to write a JavaScript function that takes a nested array containing numbers, false values, zeros, and strings, then returns the product of all valid numbers. The function should ignore zeros and falsy values while recursively processing nested arrays. Problem Statement Given a nested array with mixed data types, we want to multiply only the truthy numeric values while ignoring zeros, null, undefined, false, and strings. Solution We'll use recursion to traverse nested arrays and multiply only valid numbers: const arr = [1, 5, 2, null, [ 2, 5, ...

Read More

What is onmouseenter event in JavaScript?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 376 Views

The onmouseenter event triggers when the mouse pointer enters an HTML element. Unlike onmouseover, it doesn't bubble and only fires when entering the target element itself, not its child elements. Syntax element.onmouseenter = function() { // Code to execute }; // Or in HTML Example: Basic Mouse Enter Event Here's how to use the onmouseenter event to display an alert when hovering over text: function sayHello() { ...

Read More

Which Measurement Unit should be used in CSS to set letter spacing

Lakshmi Srinivas
Lakshmi Srinivas
Updated on 15-Mar-2026 963 Views

To set letter spacing with CSS, use the em measurement unit for most cases, though other units like px and rem are also available depending on your needs. The em unit is a relative measurement based on the font size of the current element. Because an em unit equals the size of the current font, if you assign a font to 12pt, each "em" unit would be 12pt; thus, 2em would be 24pt. Why Use em for Letter Spacing? Using em units makes letter spacing responsive and proportional to the font size. When the font size changes, ...

Read More

Difference between shift() and pop() methods in Javascript

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

The shift() method removes the element at the first index (index 0) and shifts all remaining elements down by one position, then returns the removed value. If the array is empty, it returns undefined. The pop() method removes the last element from an array and returns that element. This method changes the length of the array. Syntax array.shift() // Removes first element array.pop() // Removes last element Example: Comparing shift() and pop() let fruits = ['apple', 'mango', 'orange', 'kiwi']; let fruits2 = ['apple', 'mango', 'orange', 'kiwi']; ...

Read More

How to embed JavaScript in HTML file?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 612 Views

There are three main ways to embed JavaScript in an HTML file: inline scripts, internal scripts, and external scripts. Each method serves different purposes and has its own advantages. Method 1: Inline JavaScript You can add JavaScript directly to HTML elements using event attributes: Inline JavaScript Inline JavaScript Example Click Me Method 2: Internal JavaScript Use the tag within the HTML document to ...

Read More

Find all subarrays with sum equal to number? JavaScript (Sliding Window Algorithm)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 713 Views

We are given an array of numbers and a target sum. Our job is to write a function that returns an array of all the subarrays which add up to the target number using the sliding window algorithm. For example: const arr = [23, 5, 1, 34, 12, 67, 9, 31, 6, 7, 27]; const sum = 40; Should find these subarrays that sum to 40: [ [ 5, 1, 34 ], [ 9, 31 ], [ 6, 7, 27 ] ] The Sliding Window Algorithm The sliding window algorithm ...

Read More
Showing 18591–18600 of 61,297 articles
Advertisements