Articles on Trending Technologies

Technical articles with clear explanations and examples

JavaScript filter array by multiple strings

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

Filtering an array by multiple strings in JavaScript involves identifying elements that match any string from a given list. This is commonly used in search filters, dynamic matching, or data processing tasks. JavaScript provides simple tools like the filter method and techniques such as includes for exact matches or regular expressions for pattern-based matching. These approaches help efficiently narrow down arrays based on specific criteria. Approaches to Filter Array by Multiple Strings Here are three effective approaches to filter an array by multiple strings in JavaScript: Using filter with includes (Recommended) ...

Read More

Group values on same property - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

When working with arrays of objects, you often need to group items that share the same property value. This tutorial shows how to group objects by a common property and combine other properties. The Problem Suppose we have an array of objects with unit and brand properties: const arr = [ {unit: 35, brand: 'CENTURY'}, {unit: 35, brand: 'BADGER'}, {unit: 25, brand: 'CENTURY'}, {unit: 15, brand: 'CENTURY'}, {unit: 25, brand: 'XEGAR'} ]; console.log("Original array:"); console.log(arr); ...

Read More

How to change string to be displayed as a subscript using JavaScript?

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

In this tutorial, we will learn to display strings as subscripts using JavaScript. A subscript is text that appears smaller and positioned below the baseline of normal text. For example, in H2O, the "2" is a subscript. Subscripts are commonly used in mathematics (X1, Y2), chemistry (CO2, H2SO4), and scientific notation. Here, we'll explore both HTML and JavaScript methods to create subscripts. Using HTML Tag The simplest way to create subscripts is using the HTML tag. Any text inside this tag will be displayed as a subscript. Syntax Water is H2O ...

Read More

How to create a two dimensional array in JavaScript?

Nikitha N
Nikitha N
Updated on 15-Mar-2026 816 Views

A two-dimensional array has more than one dimension, such as myarray[0][0] for element one, myarray[0][1] for element two, etc. JavaScript doesn't have true 2D arrays, but you can create arrays of arrays to achieve the same functionality. Method 1: Using Array Constructor with Loop This method creates an empty 2D array and fills it row by row: var myarray = new Array(3); for (i = 0; ...

Read More

How to understand JavaScript module pattern?

Lokesh Badavath
Lokesh Badavath
Updated on 15-Mar-2026 351 Views

The JavaScript Module Pattern is a design pattern that encapsulates private and public methods and variables within a single object. It provides a way to create modular, maintainable code while avoiding global namespace pollution. What is the Module Pattern? The Module pattern uses Immediately Invoked Function Expressions (IIFE) to create a closure that contains private variables and functions. It returns an object with public methods that can access these private elements. Why Use the Module Pattern? Maintainability − Modules are self-contained and reduce dependencies, making code easier to maintain and improve independently. Namespace Protection − ...

Read More

Unable to take a photo from webcam using HTML5 and on the first page load

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

When taking photos from webcam using HTML5, you may encounter issues on first page load. This typically happens due to improper initialization or missing user permissions. Here's how to properly implement webcam photo capture: HTML Structure Take Photo Declare Variables var streaming = false, video = document.querySelector('#video'), canvas = document.querySelector('#canvas'), photo = document.querySelector('#photo'), startbutton = document.querySelector('#startbutton'), width = 320, height = 0; Initialize Camera ...

Read More

JavaScript date.@@toPrimitive() function

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 129 Views

The JavaScript @@toPrimitive method (accessed via Symbol.toPrimitive) converts a Date object into a primitive value based on the specified hint. This method is called internally during type coercion but can also be invoked explicitly. Syntax date[Symbol.toPrimitive](hint) Parameters The hint parameter specifies the preferred type of conversion: "default" - Returns string representation (same as toString()) "string" - Returns string representation "number" - Returns numeric value (milliseconds since Unix epoch) Example: Using Symbol.toPrimitive with Different Hints ...

Read More

Is there any way I can call the validate() function outside the initValidation() function in JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

When you define a function inside another function in JavaScript, it's only accessible within the parent function's scope. To call the validate() function outside of initValidation(), you have several approaches. The Problem In the following code, validate() is scoped inside initValidation() and cannot be accessed externally: function initValidation(){ // irrelevant code here function validate(_block){ // code here } } Method 1: Using Constructor Pattern Convert the parent function into a constructor and assign ...

Read More

Sum arrays repeated value - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 186 Views

Suppose, we have an array of objects like this − const arr = [ {'ID-01':1}, {'ID-02':3}, {'ID-01':3}, {'ID-02':5} ]; We are required to add the values for all these objects together that have identical keys Therefore, for this array, the output should be − const output = [{'ID-01':4}, {'ID-02':8}]; We will loop over the array, check for existing objects with the same keys, if they are there, we add value to it otherwise we push new objects to the ...

Read More

How to find duplicate values in a JavaScript array?

Abhishek
Abhishek
Updated on 15-Mar-2026 73K+ Views

In this tutorial, we will discuss how we can find duplicate or repeating values in a JavaScript array using different methods or approaches to reach the solution to this problem. Below is the list of the approaches or methods we can use to solve the problem of finding duplicates − By simply Traversing the Array Using the filter() and indexOf() Methods Using the Set object and has() Method Let us discuss all of the above approaches to find duplicates in the JavaScript array − By Simply Traversing the Array Traversing the array using ...

Read More
Showing 16731–16740 of 61,297 articles
Advertisements