Yaswanth Varma

Yaswanth Varma

307 Articles Published

Articles by Yaswanth Varma

Page 14 of 31

How to define custom sort function in JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 15K+ Views

Creating custom sort functions in JavaScript allows you to control exactly how array elements are ordered. By defining a custom sort function, you can sort elements in specific ways that go beyond the default string-based sorting behavior. This article will explain how to define custom sort functions in JavaScript, provide practical examples, and show different sorting techniques. Understanding the Default sort() Behavior By default, the sort() method converts elements to strings and compares them lexicographically. This can lead to unexpected results with numbers: ...

Read More

Accessing and returning nested array value - JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 2K+ Views

Each value in an array is referred to as an element, and each element has a certain numerical location in the array, which is referred to as its index. Nested Arrays, a feature of JavaScript, allow us to create arrays inside arrays. One or more arrays can be used as the elements of nested arrays. Although the term may be a little unclear, as we look further, it is actually rather fascinating. Understanding Nested Arrays In JavaScript, a nested array is described as an Array (outer array) inside another array (inner array). One or more inner arrays ...

Read More

How to open link in a new window using JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 9K+ Views

If you want users to click on links and open those links in a new window because you want your user to stay on your site while they view the linked content, then you need to use this solution. By using JavaScript you can easily configure all of your links so that they open in a new window when clicked. Approach to Open Link in a New Window Using window.open() Method Using target="_blank" with onclick Using window.open() Method JavaScript window.open() method allows you to open a new ...

Read More

How to test if a URL string is absolute or relative - JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 2K+ Views

Knowing whether a URL string is absolute or relative allows you to make decisions about content loading and routing in your JavaScript applications. In this article, we'll explore different methods to determine if a given URL string is absolute or relative. Understanding URL Types Absolute URL contains all necessary information to locate a resource, including the protocol (http/https) and domain name. Syntax https://www.tutorialspoint.com/index.htm Relative URL contains only the path information and relies on the current page's context to resolve the full location. Syntax /index.htm ../images/logo.png index.htm Method 1: ...

Read More

How to filter an array from all elements of another array – JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 645 Views

In JavaScript, filtering an array to exclude elements present in another array is a common task. This involves removing all elements from the first array that match any element in the second array. When storing multiple values in a single variable, arrays are used. Each element of an array has a numeric index that enables access to it. Arrays in JavaScript begin at index zero and can be modified using various methods. Using filter() with includes() (Recommended) The most straightforward approach combines filter() with includes() to exclude matching elements: ...

Read More

Capitalize a word and replace spaces with underscore - JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 2K+ Views

In JavaScript, you can capitalize words and replace spaces with underscores using various string methods. This transformation is commonly needed for creating identifiers, constants, or formatted text. let input = "tutorials point"; console.log("Input:", input); // Expected output: "Tutorials_Point" Input: tutorials point Let's explore different methods to achieve this transformation in JavaScript. Using replace() Method The replace() method searches for a specified pattern in a string and replaces it with a new value. It accepts regular expressions for complex pattern matching. Syntax string.replace(searchValue, newValue) Example 1: Basic ...

Read More

How to get substring between two similar characters in JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 4K+ Views

In JavaScript, extracting a substring between two similar characters is a common task when parsing strings. This article explores different methods to achieve this using built-in string methods. Understanding String Methods Before diving into the solution, let's understand the key methods we'll use: The substring() method extracts characters between two indices and returns a new string without modifying the original. Syntax string.substring(start, end) The split() method divides a string into an array of substrings based on a specified separator. Syntax string.split(separator, limit) Method 1: Using split() ...

Read More

Sorting an array that contains undefined in JavaScript?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 3K+ Views

In JavaScript, arrays can contain undefined values which require special handling during sorting. By default, the sort() method converts all elements to strings, placing undefined values at the end of the sorted array. Let's explore different approaches to sort arrays containing undefined values effectively. Default Behavior with sort() When using the basic sort() method on arrays with undefined, JavaScript automatically places undefined values at the end: var namearr = ["Zebra", "Yatch", undefined, "Egg", undefined]; namearr.sort(); ...

Read More

How do you display JavaScript datetime in 12hour AM/PM format?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 4K+ Views

The most practical way to display datetime for efficient time analysis is in a 12-hour AM/PM format. This method clarifies the distinction between morning and evening, making times like "2:30 PM" more intuitive than "14:30" in 24-hour format. This article will explain various methods for displaying JavaScript datetime in 12-hour AM/PM format. Using the toLocaleString() Method The toLocaleString() method returns a formatted date string according to the specified locale and options. It's the most straightforward approach for 12-hour format conversion. Syntax Date.toLocaleString(locales, options) Parameters locales − Language/region ...

Read More

Substitute random items in a JavaScript array?

Yaswanth Varma
Yaswanth Varma
Updated on 15-Mar-2026 452 Views

To substitute random items in a JavaScript array, you can use Math.random() along with map() to replace selected elements with new values while keeping others in their original positions. For example, if you have an array [m, n, o, p] and want to replace two random items with 'a', the result might be [a, a, o, p] where m and n were randomly selected and replaced. Using Math.random() Method The Math.random() method returns a pseudo-random number between 0 (inclusive) and 1 (exclusive). It's used to generate random indices for array element selection. Syntax Math.random(); ...

Read More
Showing 131–140 of 307 articles
« Prev 1 12 13 14 15 16 31 Next »
Advertisements