Articles on Trending Technologies

Technical articles with clear explanations and examples

Getting elements of an array depending on corresponding values of another JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 685 Views

Suppose we have two arrays, for example consider the following two: const array1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; const array2 = [ 1, 0, 0, 1 , 0, 0, 1, 0]; Both arrays are bound to have the same length. We need to write a function that, when provided with an element from the second array, returns a subarray from the first array containing all elements whose index corresponds to the index of the element we took as an argument in the second array. For example: findSubArray(0) should return: ...

Read More

addEventListener() not working more than once with a button in JavaScript?

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

The addEventListener() method works multiple times by design. If it seems to work only once, the issue is usually in your event handler function or how you're setting up the listener. Common Problem: Removing the Element A frequent mistake is removing or replacing the button element inside the event handler, which destroys the event listener: addEventListener Problem Bad Example - Works Once ...

Read More

How to divide an unknown integer into a given number of even parts using JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 530 Views

When dividing an integer into equal parts, you may need to distribute the remainder across some parts to ensure all values are used. This can be achieved using the modular operator and array manipulation. How It Works The algorithm divides the integer by the number of parts to get a base value. If there's a remainder, it distributes the extra values by adding 1 to some parts. Example function divideInteger(value, parts) { var baseValue; var remainder = value % parts; ...

Read More

Casting a string to snake case - JavaScript

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

Snake case is a naming convention where words are separated by underscores (_) and all letters are lowercase. For example, "Hello World" becomes "hello_world". This article demonstrates how to convert any string to snake case using JavaScript. Syntax function toSnakeCase(str) { return str.split(' ') .map(word => word.toLowerCase()) .join('_'); } Example Here's a complete example that converts a sentence to snake case: ...

Read More

How to deal with floating point number precision in JavaScript?

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

Floating point precision issues occur when JavaScript cannot exactly represent certain decimal numbers in binary format. Common problems include 0.1 + 0.2 = 0.30000000000000004. JavaScript provides several methods to handle these precision issues. The Floating Point Problem JavaScript uses IEEE 754 double-precision floating-point format, which can cause unexpected results: console.log(0.1 + 0.2); // 0.30000000000000004 console.log(0.1 + 0.2 === 0.3); // false ...

Read More

How to find the text visible on the button with JavaScript?

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

In this tutorial, we will discuss different approaches to find the text visible on the button with JavaScript. The text on a button shows what will happen if you click the button. To extract the text on the button, JavaScript provides us with the following two properties. Using the innerHTML Property Using the innerText Property Let us discuss both of the above properties in detail. Using the innerHTML Property The innerHTML property not only allows us to get the text inside any tag but also allows us to set the text ...

Read More

What is the best JavaScript code to create an img element?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 311 Views

Creating an image element in JavaScript can be done in several ways. The most common approach is using document.createElement() or the Image() constructor. Using document.createElement() The standard DOM method to create an image element: Create Image Element // Create image element var myImg = document.createElement('img'); myImg.src = 'https://via.placeholder.com/150x100'; ...

Read More

Enable rear camera with HTML5

Samual Sam
Samual Sam
Updated on 15-Mar-2026 540 Views

To enable the rear camera in HTML5, you need to access the device's camera sources and specify which camera to use. This is particularly useful on mobile devices that have both front and rear cameras. Getting Available Camera Sources First, use the MediaDevices.enumerateDevices() method to get all available media devices: Rear Camera Access Start Rear Camera async function getRearCamera() { ...

Read More

Usage of border-style property in CSS

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 152 Views

The border-style property in CSS defines the style of an element's border. It controls how the border appears visually, such as solid, dashed, dotted, or hidden. Syntax border-style: value; Common Border Style Values Value Description none No border (default) solid Solid single line dashed Dashed line dotted Dotted line double Double solid lines groove 3D grooved effect ridge 3D ridged effect inset 3D inset effect outset 3D outset effect Basic Example ...

Read More

Adding an element at the end of the array in Javascript

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

In JavaScript, adding elements to the end of an array is a fundamental operation. The most common and efficient method is using the push() method, which modifies the original array by appending one or more elements. An array is a special variable that can hold multiple values in a single ordered collection. Arrays in JavaScript are dynamic, meaning you can add or remove elements after creation. What is an Array? An array is a collection of items stored at contiguous memory locations. Arrays allow random access to elements, making it faster to access elements by their position ...

Read More
Showing 17251–17260 of 61,299 articles
Advertisements