Sling Academy
Home/JavaScript/Page 85

JavaScript

JavaScript: How to iterate through 2 arrays in parallel

Updated: Mar 08, 2023
Iterating through 2 arrays in parallel means iterating over 2 arrays at the same time and accessing the corresponding elements of each array. This article will show you how to do that in JavaScript. Using a Loop Let’s say you......

JavaScript: Split an array into equally-sized chunks (3 approaches)

Updated: Mar 08, 2023
This concise, straight-to-the-point article walks you through a couple of different ways to split a given JavaScript array into smaller equally-sized chunks (subarrays). Note that the final chunk may have fewer elements than other chunks.......

JavaScript: 4 Ways to Flatten an Array

Updated: Mar 08, 2023
This succinct, example-based article will walk you through 4 different approaches to flattening a nested array in JavaScript. No need to procrastinate anymore; let’s roll up our sleeves and start! Using the flat() method The......

JavaScript: Get an array of dates between 2 given dates

Updated: Mar 08, 2023
In JavaScript, to get all dates between 2 given dates as an array, we can use a loop to iterate over the dates and push each date to an array until we reach the end date. We can use the push() method to add the dates to the array. This......

JavaScript: Display a Date object in 12-hour format (am/pm)

Updated: Mar 08, 2023
This article shows you a couple of different ways to display a date object in 12-hour format, with AM and PM, in JavaScript. Note: AM stands for “ante meridiem” which means “before noon” in Latin, and PM stands......
Javascript: Change Background Color on Scroll

Javascript: Change Background Color on Scroll

Updated: Mar 08, 2023
The example below shows you how to dynamically change the background color of a webpage when the user scrolls down. We only use vanilla Javascript and CSS to make this. Example Preview Here’s the demo: The code The......

JavaScript: 2 Ways to Clone (Deep Copy) a Date Object

Updated: Mar 07, 2023
In JavaScript, Date objects are mutable, so if you simply assign a Date object to a new variable, changes to the new variable will affect the original Date object. This article will walk you through a couple of different ways to create a......

JavaScript: 3 ways to check if a date string is valid

Updated: Mar 07, 2023
Some common valid date string formats in JavaScript are: ISO 8601 format: 2023-10-30 or 2023-10-30T16:30:00Z Short date format: 11/30/2023 Long date format: October 30 2023 or 30 October 2022 RFC 2822 format: 30 Oct 2023......

JavaScript: Sorting an Array of Objects by Date Property

Updated: Mar 07, 2023
To sort an array of objects by date property in JavaScript, we can use the Array.prototype.sort() method with a custom comparator function. The comparator function should compare the date values of the objects using the Date constructor......

JavaScript: 3 ways to truncate the time portion of a date string

Updated: Mar 07, 2023
When building applications with JavaScript, there might be scenarios where you want to remove the time portion from a date string, such as: Displaying dates: When displaying dates in a UI, you may only want to show the date part and......

JavaScript: How to Check if 2 Date Ranges Overlap (2 Ways)

Updated: Mar 07, 2023
A date range refers to a period of time that is defined by two dates: a start date and an end date. For example, a date range can be defined as January 1, 2023, to January 31, 2023. When developing applications with modern JavaScript,......
JavaScript: How to Detect Dark/Light Mode

JavaScript: How to Detect Dark/Light Mode

Updated: Mar 07, 2023
Knowing that dark or light mode is being applied is useful for websites (and web apps) to programmatically adjust their UI based on the user’s preference or the device’s theme. This practical article will walk you through a......