Articles on Trending Technologies

Technical articles with clear explanations and examples

The HashTable Class in Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Mar-2026 213 Views

A HashTable (Hash Map) is a data structure that stores key-value pairs using a hash function to compute array indices. This implementation uses chaining to handle collisions, where multiple values can be stored at the same index using arrays. Complete HashTable Implementation class HashTable { constructor() { this.container = []; // Populate the container with empty arrays // which can be used to add more elements in // cases of collisions for (let i = ...

Read More

PreventDefault( ) vs Return false in JavaScript?

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

In JavaScript, when handling events, you often need to prevent the default browser behavior. The two common approaches are preventDefault() and return false, but they work differently. The preventDefault() method stops only the default action, while return false stops both the default action and event propagation. Key Differences preventDefault() return false Method available on event objects JavaScript statement that can be used anywhere Stops only the default behavior Stops default behavior AND event propagation Allows event to continue bubbling up Stops event bubbling and function execution ...

Read More

Is there an elegant way of passing an object of parameters into a function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 126 Views

Passing an object of parameters to a function provides flexibility and improves code readability. This technique allows you to avoid positional parameters and makes your functions more maintainable. Using Object Destructuring (Recommended) The most elegant approach uses ES6 destructuring to extract object properties directly in the function parameter: Object Parameters Example Passing Object Parameters to Functions Calculate Sum ...

Read More

Remove and add new HTML Tags with JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 809 Views

JavaScript provides several methods to dynamically remove and add HTML elements to the DOM. You can hide/show existing elements or completely remove/create new elements using vanilla JavaScript or jQuery. Method 1: Using jQuery hide() and show() The simplest approach is using jQuery's hide() and show() methods to toggle element visibility: Hide/Show Elements Test JavaScript This content can be hidden and shown. ...

Read More

Check for illegal number with isNaN() in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 252 Views

In JavaScript, isNaN() function checks if a value is "Not a Number" (NaN). It's commonly used to validate numeric operations and detect illegal numbers in calculations. What is NaN? NaN stands for "Not a Number" and is returned when a mathematical operation fails or produces an undefined result, such as multiplying a string with a number. Syntax isNaN(value) Parameters value: The value to be tested. If not a number, JavaScript attempts to convert it before testing. Return Value Returns true if the value is NaN, false otherwise. Example: Detecting ...

Read More

Finding mistakes in a string - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 543 Views

We are required to write a JavaScript function that takes in two strings. The first string is some mistyped string and the second string is the correct version of this string. We can assume that the two strings we are getting as argument will always have the same length. We have to return the number of mistakes that exist in the first string by comparing it character by character with the correct version. Approach The solution involves iterating through both strings simultaneously and comparing each character at the same position. When characters don't match, we increment our ...

Read More

Thrice sum of elements of array - JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as sum of three consecutive elements from the original array. For example, if the input array is: const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Then the output should be: const output = [3, 12, 21, 9]; The function groups elements in sets of three and calculates their sum: (0+1+2=3), (3+4+5=12), (6+7+8=21), and (9+0+0=9) for the remaining element. How It Works The ...

Read More

How to add 2 hours to a JavaScript Date object?

Saurabh Jaiswal
Saurabh Jaiswal
Updated on 15-Mar-2026 29K+ Views

In this tutorial, we will learn how to add 2 hours to a JavaScript Date object. Here we will discuss two methods which are following. Using the getTime() Method Using the setHours() Method Using the getTime() Method JavaScript date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime() method is the number of milliseconds since 1 January 1970 at 00:00:00. Syntax Date.getTime() Approach To add 2 hours to the Date Object first, we get ...

Read More

Which event occurs in JavaScript when an element's content is cut?

V Jyothi
V Jyothi
Updated on 15-Mar-2026 196 Views

The oncut event occurs when an element's content is cut using Ctrl+X or the right-click context menu. This event is commonly used with input fields, textareas, and other editable elements to track when users cut text. Syntax element.oncut = function() { // Code to execute when content is cut }; // Or using addEventListener element.addEventListener('cut', function(event) { // Code to execute when content is cut }); Example: Basic oncut Event Cut Event Example ...

Read More

Draw part of an image inside HTML5 canvas

Rishi Rathor
Rishi Rathor
Updated on 15-Mar-2026 583 Views

Drawing part of an image in HTML5 canvas is useful for creating sprites, animations, or displaying specific portions of larger images. The key is using the extended drawImage() method with clipping parameters. Syntax context.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); Parameters Parameter Description sx, sy Starting coordinates in source image sWidth, sHeight Width and height of clipped area in source dx, dy Destination coordinates on canvas dWidth, dHeight Size to draw on canvas Example: Drawing Part of an Image ...

Read More
Showing 18111–18120 of 61,297 articles
Advertisements