How to Calculate the Days Between Two Dates in JavaScript

If you are working with JavaScript and need to calculate the number of days between two given dates, you can use the following function. This function takes two JavaScript Date objects as parameters and returns an array of Date objects, representing each day between the two dates. const getDatesBetweenDates = (startDate, endDate) => { let dates = []; // initialize an empty array to store the dates const theDate = new Date(startDate); // create a copy of the start date to avoid modification while (theDate < endDate) { // add the current date to the array dates = [....

How to Check if a Date Refers to a Past Day in JavaScript

When working with dates in JavaScript, you may come across a scenario where you need to determine if a given date refers to a day in the past. Simply comparing dates using the getTime() method isn’t sufficient, as dates may have different times associated with them. This article will guide you through a solution to this problem. To check if a date references a past day in JavaScript, you can use the following function:...

How to check if a string contains a substring in JavaScript

Checking if a string contains a substring is a common task in JavaScript programming. In this blog, we will explore different ways to accomplish this and find the best approach to use. The simplest and preferred method is to use the includes() method on a string. Introduced in ES6/ES2015, this method returns true if the string contains the specified substring, and false otherwise. 'a nice string'.includes('nice') // true Please note that includes() is supported in all modern browsers except Internet Explorer....

How to Check if a String Starts with Another in JavaScript

Checking if a string starts with another substring is a common task in JavaScript. In this article, we will explore how to perform this check efficiently using the startsWith() method introduced in ES6. ES6, released in 2015, introduced the startsWith() method, making it easier to check if a string starts with a specified substring. This method is now the preferred approach in modern JavaScript. To use startsWith(), simply call the method on any string instance and pass in the substring you want to check....

How to Check if an Object is Empty in JavaScript

Learn how to determine if a variable corresponds to an empty object. If you need to check whether a value is equal to an empty object created using the object literal syntax const emptyObject = {}, there are several ways to do it. One method is by using the Object.entries() function, which returns an array containing the enumerable properties of an object. By calling Object.entries(objectToCheck), you can check if it returns an empty array....

How to Check if Two Dates are the Same Day in JavaScript

Are you looking to determine if two date object instances in JavaScript refer to the same day? JavaScript doesn’t come with built-in functionality for this, but you can easily implement it using a few methods. The getDate() method returns the day, the getMonth() method returns the month, and the getFullYear() method returns the four-digit year. By comparing these values between two date objects, you can check if they represent the same day....

How to Check Types in JavaScript without Using TypeScript

If you’re familiar with TypeScript, you know that it’s a language introduced by Microsoft that adds types to JavaScript. While TypeScript can be beneficial, especially for larger projects, it may not always be the best choice for beginners or for those who prefer to stick to the fundamentals of the Web Platform. However, there are times when having types in JavaScript can be helpful. Fortunately, there is a way to add types to JavaScript without using TypeScript, and it involves using VS Code....

How to Click a Link with Specific Text Using Puppeteer

In this tutorial, we will explore how to click a link with specific text using Puppeteer, a powerful Node.js library for controlling headless Chrome or Chromium. When working with web automation, it is often necessary to interact with buttons or links on a page. In this specific case, let’s say we want to click an “Accept all” cookie button. To accomplish this task, we can use the following code snippet:...

How to Clone Anything in JavaScript

In the world of JavaScript, cloning objects has always been a common task. Why? The answer lies in references. When it comes to primitive types like strings, numbers, and booleans, cloning is as simple as passing them by value. However, with other types such as objects, arrays, and dates, things get a bit trickier since they are passed by reference. Without proper cloning, you end up with multiple references to the same object under different names....

How to Count the Number of Properties in a JavaScript Object

Learn how to efficiently calculate the number of properties in a JavaScript object. To accomplish this, you can use the Object.keys() method. By passing the object you want to inspect as the argument, you can obtain an array that contains all the enumerable (own) properties of the object. To count the number of properties, simply access the length property of the array generated by Object.keys(): const car = { color: 'Blue', brand: 'Ford', model: 'Fiesta' } Object....