JavaScript

4 Ways to Check If a String Contains a Substring in JavaScript

The Short Answer There are several ways to check if a JavaScript string contains a substring. Here’s a short summary of the most notable ones. 1. string.indexOf() Use the indexOf() method. This method returns the index of the first occurrence of the substring in the string, or -1 if the substring is not found. For …

4 Ways to Check If a String Contains a Substring in JavaScript Read More »

JavaScript How to Find Duplicates in Array (without Removing)

To find (and not delete) duplicates in a JavaScript array, you can use this function: Example use: Output: To learn how the above approach works, and what alternatives exist, please keep on reading. This guide shows you ways to find array duplicates and show them instead of deleting them. Besides, you will learn what performance …

JavaScript How to Find Duplicates in Array (without Removing) Read More »

JavaScript call() vs apply(): A Complete Guide (Examples)

In JavaScript, the call() and apply() methods are both used to borrow and call a function on another object. The only difference between them is the way they accept arguments: Both methods are useful when borrowing functionality. To call a function of one object on another object you can use call() or apply(). This is …

JavaScript call() vs apply(): A Complete Guide (Examples) Read More »

JavaScript ‘throw new Error’ vs ‘throw Error’ vs ‘throw something’

In JavaScript the difference between throw Error and throw new Error is that: When it comes to behavior, there’s really no difference between throw Error and throw new Error. For example: But the more technically correct way to throw a new Error object is by using the throw new Error instead of throw Error. Also, …

JavaScript ‘throw new Error’ vs ‘throw Error’ vs ‘throw something’ Read More »