Articles on Trending Technologies

Technical articles with clear explanations and examples

Adding an element in an array using Javascript

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

Adding an element to an array can be done using different functions for different positions. Adding an element at the end of the array This can be accomplished using the push method. For example, let veggies = ["Onion", "Raddish"]; veggies.push("Cabbage"); console.log(veggies); This will give the output − ["Onion", "Raddish", "Cabbage"] You can also use this to push multiple items at the same time as it supports a variable number of arguments. For example, let veggies = ["Onion", "Raddish"]; veggies.push("Cabbage", "Carrot", "Broccoli"); console.log(veggies); This will give the ...

Read More

Breaking a loop in functional programming JavaScript.

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 204 Views

In traditional loops, we use break to exit early. In functional programming, JavaScript provides array methods like some() and every() that can terminate iteration based on conditions. Using Array.some() to Break Early The some() method stops iterating when the callback returns true, making it perfect for early termination: Breaking Loop Example body { font-family: ...

Read More

Array sum: Comparing recursion vs for loop vs ES6 methods in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 331 Views

When working with arrays in JavaScript, there are multiple ways to calculate the sum of all elements. Let's compare three popular approaches: recursion, traditional for loops, and ES6 methods like reduce(). To demonstrate performance differences, we'll test each method with a large number of iterations. This gives us insights into which approach performs best for array summation tasks. Recursive Approach The recursive method calls itself until it processes all array elements: const recursiveSum = (arr, len = 0, sum = 0) => { if(len < arr.length){ ...

Read More

Check if a string has white space in JavaScript?

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

To check if a string contains whitespace in JavaScript, you can use several methods. The most common approaches are indexOf(), includes(), and regular expressions. Using indexOf() Method The indexOf() method returns the index of the first whitespace character, or -1 if none is found: function stringHasTheWhiteSpaceOrNot(value){ return value.indexOf(' ') >= 0; } var whiteSpace = stringHasTheWhiteSpaceOrNot("MyNameis John"); if(whiteSpace == true){ console.log("The string has whitespace"); } else { console.log("The string does not have whitespace"); } // Test with different strings console.log(stringHasTheWhiteSpaceOrNot("HelloWorld")); ...

Read More

How to print a number with commas as thousands of separators in JavaScript?

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

In this tutorial, we will look at a few ways to print a number with commas as thousands of separators in JavaScript and compare them to understand which one is suitable in a given context. Why do we do number formatting? In English, commas are used to separate numbers bigger than 999. Dot is the thousands separator in Germany. Sweden uses space. The separator is added after every three digits from the right. Let's briefly introduce the methods. Using the toLocaleString() Method Here, the built-in locale string method localizes the number format based on the country. ...

Read More

How to access document properties using W3C DOM method?

Sravani S
Sravani S
Updated on 15-Mar-2026 232 Views

The W3C DOM (Document Object Model) provides standardized methods to access and manipulate HTML document properties. Unlike browser-specific approaches, W3C DOM methods work consistently across all modern browsers. Common W3C DOM Methods The most frequently used methods for accessing document properties include: getElementById() - Gets element by its ID attribute getElementsByTagName() - Gets elements by tag name getElementsByClassName() - Gets elements by class name querySelector() - Gets first element matching CSS selector Example: Accessing Document Properties Here's how to access various document properties using W3C DOM methods: ...

Read More

How to find the coordinates of the cursor with JavaScript?

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

In this tutorial, we will learn how to find the coordinates of the mouse cursor with JavaScript. We need to determine the X and Y coordinates (horizontal and vertical positions) of the cursor on the screen when mouse events occur. JavaScript provides us with two essential properties to get the coordinates of the mouse cursor when events are triggered: Using event.clientX Property Using event.clientY Property Using event.clientX Property The event.clientX property returns the horizontal position (X-coordinate) of the cursor relative to the browser's viewport when an event is ...

Read More

HTML 5 video or audio playlist

Giri Raju
Giri Raju
Updated on 15-Mar-2026 880 Views

HTML5 provides the onended event that fires when audio or video playback completes. This event is essential for creating video playlists, showing completion messages, or automatically loading the next media file. Basic onended Event Example The onended event allows you to execute JavaScript when media playback finishes. Here's a simple example that shows a message: Your browser does not support the video ...

Read More

Descendent Selectors in CSS

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

CSS descendant selectors allow you to apply styles to elements that are nested inside other elements. This targeting method helps create specific styling rules without affecting similar elements elsewhere on the page. Syntax ancestor descendant { property: value; } The descendant selector uses a space between two selectors to target elements that are children, grandchildren, or any level deep within the ancestor element. Example: Styling Emphasized Text in Lists Apply yellow color to elements only when they appear inside tags: ul ...

Read More

Count groups of negatives numbers in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 460 Views

We have an array of numbers like this: const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0]; Let's say, we are required to write a JavaScript function that counts the consecutive groups of negative numbers in the array. Like here we have consecutive negatives from index 0 to 2 which forms one group and then from 4 to 8 forms the second group. So, for this array, the function should return 2. Understanding the Problem A group of consecutive negative numbers is defined as: One ...

Read More
Showing 18451–18460 of 61,297 articles
Advertisements