Web Development Articles

Page 209 of 801

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

Finding and returning uncommon characters between two strings in JavaScript

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

Problem We are required to write a JavaScript function that takes in two strings. Our function should return a new string of characters which is not common to both the strings. Example Following is the code − const str1 = "xyab"; const str2 = "xzca"; const findUncommon = (str1 = '', str2 = '') => { const res = []; for (let i = 0; i < str1.length; i++){ if (!(str2.includes(str1[i]))){ ...

Read More

JavaScript - Accept only numbers between 0 to 255 range?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 740 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 2K+ 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 647 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

Returning the value of nth power of iota(i) using JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 175 Views

In mathematics, the imaginary unit i is defined as the square root of -1. When calculating powers of i, the results follow a cyclic pattern that repeats every 4 powers. Mathematical Background The imaginary unit i has the following properties: i = √(-1) i² = -1 i³ = -i i⁴ = 1 Since i⁴ = 1, the pattern repeats every 4 powers. This means we can use the modulo operator to determine the result for any power of i. Power Pattern Power (n % 4) Result (iⁿ) 0 ...

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 419 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

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 594 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
Showing 2081–2090 of 8,010 articles
« Prev 1 207 208 209 210 211 801 Next »
Advertisements