Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 209 of 801
Convert set to object - JavaScript?
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 MoreFinding and returning uncommon characters between two strings in JavaScript
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 MoreJavaScript - Accept only numbers between 0 to 255 range?
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 MoreHow to define custom sort function in JavaScript?
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 MoreHow to test if a URL string is absolute or relative - JavaScript?
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 MoreHow to filter an array from all elements of another array – JavaScript?
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 MoreReturning the value of nth power of iota(i) using JavaScript
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 MoreHow to make a condition for event 'click' inside/outside for multiple divs - JavaScript?
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 MoreWhat happens when length of object is set to 0 - JavaScript?
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 MoreFrequency of elements of one array that appear in another array using JavaScript
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