Saurabh Anand

Saurabh Anand

17 Articles Published

Articles by Saurabh Anand

17 articles

JavaScript Program for Equilibrium Index of an Array

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 878 Views

To write a JavaScript program for equilibrium index of an array, we are going to discuss three different approaches with detailed explanation and example codes. The equilibrium index of an array is the position where the sum of the elements to its left and right equals one another. In this article we are having an array of integers, our task is to write a JavaScript program for equilibrium index of an array. Example Input: arr = [-7, 1, 5, 2, -4, 3, 0] Output: Equilibrium index: 3 At index 3, left sum = -7 ...

Read More

JavaScript Program to Find k Pairs with Smallest Sums in Two Arrays

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 409 Views

To find k pairs with smallest sums in two arrays in JavaScript, we will be using sorted arrays in ascending order. We are going to discuss two different approaches with stepwise explanation and examples. In this article we are having two arrays and a value of k, our task is to find k pairs with smallest sums in two arrays. Users must be familiar with JavaScript arrays, loops and conditional statement. Example Input: arr1 = [1, 7, 11], arr2 = [2, 4, 6] k = 3 Output: [[1, 2], [1, 4], [1, 6]] Input: ...

Read More

JavaScript Program for Find the smallest missing number

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 442 Views

We are given a sorted array of distinct non-negative integers, here we have to find the smallest missing number. Hence in this tutorial, we will explore different methods to solve this problem and discuss their time complexities with various examples. Understanding the Problem The problem statement is quite simple. Given a sorted array of distinct non-negative integers, we need to find the smallest missing number in it. Let's take an example to understand the problem. Example Let's say that we have an array [1, 2, 4, 5, 6]. Here, We can see that there is a ...

Read More

Error.prototype.toString() Method in JavaScript

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 2K+ Views

JavaScript's Error.prototype.toString() method is a built-in method that converts error objects to readable string representations. This method is essential for error handling, logging, and debugging in JavaScript applications. Error.prototype.toString() Method The Error.prototype.toString() method converts an error object to a string format. It returns a string containing the error name, followed by a colon and space, then the error message. For example, a standard Error object will produce a string like "Error: something went wrong". Syntax errorObject.toString() Parameters: None Return Value: A string representation of the error object Basic Usage Example Here's ...

Read More

Error: Permission denied to access property 'target' in JavaScript

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 4K+ Views

"Error − Permission denied to access property 'target'" is a common error message that occurs when working with JavaScript, especially when attempting to access the target property of an event object. This error occurs when a script tries to access a property or method of an object that it does not have permission to access. In this tutorial, we will discuss the causes of this error and how to fix it. Understanding the Error The "Error − Permission denied to access property 'target'" is typically caused by the use of the target property of an event object in ...

Read More

ES2022 JavaScript at() Method

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 319 Views

JavaScript ES2022 introduced the at() method for strings, providing a more robust way to retrieve characters at specific positions. Unlike the traditional charAt() method, at() supports negative indexing and handles edge cases more gracefully. Syntax string.at(index) Where string is the target string and index is the position (zero-based). Returns the character at the specified index, or undefined if the index is out of range. Basic Usage Here's how to retrieve the first character of a string: ...

Read More

How to convert special characters to HTML in Javascript?

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 6K+ Views

Special characters like , and & have special meaning in HTML and can break your webpage if not properly escaped. JavaScript provides several methods to convert these characters into their HTML entity equivalents. Why Convert Special Characters to HTML? When displaying user input or dynamic content on a webpage, special characters can cause problems. For example, if you want to display "" as text, the browser might interpret it as an actual HTML tag. Converting these characters to HTML entities like ensures they display as intended text. Common characters that need escaping: ...

Read More

How to convert UTC date time into local date time using JavaScript?

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 32K+ Views

Timezone handling is an essential component of every web application. The time that is recorded in the backend is usually in UTC format. When it is displayed to the user, however, it must be converted to the user's local time. This is possible with JavaScript. We'll look at how to use JavaScript to convert UTC date time to local date time in this blog. JavaScript Date Object JavaScript includes a "Date" object that allows us to work with dates and times. The Date object includes various methods for working with dates and times, including: ...

Read More

How to count text lines inside of a DOM element?

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 3K+ Views

Introduction The DOM (Document Object Model) is an API for HTML and XML documents that provides programmers control over a webpage's structure, appearance, and content. Counting text lines within DOM elements is useful for layout calculations, pagination, and content management. This article explores three methods to count text lines in DOM elements using JavaScript. Method 1: Using the scrollHeight Property The scrollHeight property returns the total height of an element, including content hidden by overflow. We can calculate the number of lines by dividing scrollHeight by the height of a single line of text. ...

Read More

How to count the number of times a button is clicked using JavaScript?

Saurabh Anand
Saurabh Anand
Updated on 15-Mar-2026 18K+ Views

Tracking button clicks is a common task in JavaScript and can be achieved by using the addEventListener() method. By adding an event handler to the button element and incrementing a variable each time the button is pressed, we can keep track of button clicks. We can display the count to users and even persist clicks using localStorage so they remain after browser closure. This technique is essential for interactive web applications like calculators, games, or analytics tracking. Let's explore different methods to count button clicks effectively. Creating a Basic Button First, we need a button element on ...

Read More
Showing 1–10 of 17 articles
« Prev 1 2 Next »
Advertisements