Front End Technology Articles

Page 208 of 652

Convert set to object - JavaScript?

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

The task we are going to perform in this article is converting a JavaScript Set to an object. Before we jump into the methods, let's have a quick overview of Sets and objects in JavaScript. A JavaScript Set is a collection of unique values where each value can only appear once. An object in JavaScript is a data structure with properties and values, similar to a dictionary or map. Converting Sets to objects is useful when you need to work with key-value pairs instead of just values. There are several methods to convert a Set to an object ...

Read More

Frequency of elements of one array that appear in another array using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 598 Views

We need to write a JavaScript function that takes two arrays of strings and returns the frequency count of each element from the second array as it appears in the first array. Problem Given two arrays, we want to count how many times each string from the second array appears in the first array. The result should be an array of counts corresponding to each element in the second array. Example Following is the code − const arr1 = ['abc', 'abc', 'xyz', 'cde', 'uvw']; const arr2 = ['abc', 'cde', 'uap']; const findFrequency = ...

Read More

JavaScript - Accept only numbers between 0 to 255 range?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 751 Views

In JavaScript, validating user input to accept only numbers within a specific range is a common requirement. This is particularly useful for form validation, color values (RGB components), or any scenario where you need to ensure numeric input falls within defined bounds. In this article, we will explore different methods to accept only numbers between 0 and 255 range using JavaScript. This range is commonly used for RGB color values, where each color component must be between 0 and 255. Using if else Condition The if/else statement executes a block of code when a condition is true, ...

Read More

How to define custom sort function in JavaScript?

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

Creating custom sort functions in JavaScript allows you to control exactly how array elements are ordered. By defining a custom sort function, you can sort elements in specific ways that go beyond the default string-based sorting behavior. This article will explain how to define custom sort functions in JavaScript, provide practical examples, and show different sorting techniques. Understanding the Default sort() Behavior By default, the sort() method converts elements to strings and compares them lexicographically. This can lead to unexpected results with numbers: ...

Read More

How to test if a URL string is absolute or relative - JavaScript?

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

Knowing whether a URL string is absolute or relative allows you to make decisions about content loading and routing in your JavaScript applications. In this article, we'll explore different methods to determine if a given URL string is absolute or relative. Understanding URL Types Absolute URL contains all necessary information to locate a resource, including the protocol (http/https) and domain name. Syntax https://www.tutorialspoint.com/index.htm Relative URL contains only the path information and relies on the current page's context to resolve the full location. Syntax /index.htm ../images/logo.png index.htm Method 1: ...

Read More

How to filter an array from all elements of another array – JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 657 Views

In JavaScript, filtering an array to exclude elements present in another array is a common task. This involves removing all elements from the first array that match any element in the second array. When storing multiple values in a single variable, arrays are used. Each element of an array has a numeric index that enables access to it. Arrays in JavaScript begin at index zero and can be modified using various methods. Using filter() with includes() (Recommended) The most straightforward approach combines filter() with includes() to exclude matching elements: ...

Read More

Maximum absolute difference of the length of strings from two arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 330 Views

Problem We are required to write a JavaScript function that takes in two arrays, a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array. Our function should find the value of − max(abs(length(x) − length(y))) Approach To find the maximum absolute difference, we need to: Find the longest string from both arrays Find the shortest string from both arrays ...

Read More

How to make a condition for event 'click' inside/outside for multiple divs - JavaScript?

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

In JavaScript, you can detect clicks inside or outside multiple divs using event listeners and the contains() method. This is useful for dropdowns, modals, or interactive UI components. The Problem When working with multiple divs, you need to determine if a click event occurred inside any of the target divs or outside all of them. This requires checking the event target against all div elements. Solution Using Event Delegation Add a single event listener to the document and check if the clicked element is contained within any target div: ...

Read More

What happens when length of object is set to 0 - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

In JavaScript, setting an array's length property to 0 immediately removes all elements and clears the array. This is an efficient way to empty an array without creating a new one. Initial Array Example Let's start with an array containing some elements: var arrayObject = [ "John", "David", "Mike" ]; console.log("Original array:", arrayObject); console.log("Original length:", arrayObject.length); Original array: [ 'John', 'David', 'Mike' ] Original length: 3 Setting Length to 0 When you set length = 0, ...

Read More

Repeating each character number of times their one based index in a string using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 612 Views

We need to write a JavaScript function that takes a string of lowercase English letters and transforms it according to specific rules: each character should be repeated based on its 1-based index position, with the first letter capitalized, and character groups separated by dashes. Problem Statement Given a string of lowercase English alphabets, construct a new string where: Each character is repeated according to its 1-based index (1st char repeated 1 time, 2nd char repeated 2 times, etc.) The first letter of each repeated group is capitalized Different character groups are separated by dashes ('-') ...

Read More
Showing 2071–2080 of 6,519 articles
« Prev 1 206 207 208 209 210 652 Next »
Advertisements