How to Add Days to Date in JavaScript

How to Add Days to Date in JavaScript

Here are two ways to add days to date in JavaScript. Using “setDate() and getDate()” methods Using the “date-fns package’s addDays()” method Method 1: Using setDate() and getDate() methods To add days to a date in JavaScript, use the “getDate()” method on the Date to get the day of the month, then call the “setDate()” … Read more

How to Append an Array to Another Array in JavaScript

How to Add Array to Array in JavaScript

To append an array to another array in JavaScript, you can use the “array.concat()”, “array.push()”, or “spread operator”. Method 1: Using the array.concat() function The array concat() is a built-in method that concatenates two or more arrays. The concat() function does not change the existing arrays but returns a new array containing the values of the … Read more

3 Easy Ways to Merge Arrays in JavaScript

How to Combine Arrays in JavaScript

Here are three ways to merge arrays in JavaScript: Using the “Spread Operator(…)”. Using the “Array.prototype.concat()” Method. Using the “Array.prototype.push()” Method. Method 1: Using the Spread Operator(…) The spread is a built-in JavaScript operator(…) used to expand or spread an iterable or an array. The spread operator allows us to copy all or part of … Read more

How to Convert an Array to Set in JavaScript

How to Convert JavaScript Array to Set

Here are four ways to convert an array to a set in JavaScript. Using Set constructor Using Array.prototype.map() Using Array.prototype.forEach() Using Array.prototype.reduce() Method 1: Using Set constructor The easiest way to convert an array to a Set in JavaScript is to “use the Set() constructor.” let moneyheist = [“Tokyo”, “Nairobi”, “Berlin”, “Oslo”, “Moscow”] let moneyheist_set = new … Read more

How to Format Number as a Currency in JavaScript

How to Convert Number to Currency Format in JavaScript

To format numbers as a currency in JavaScript, use the Intl.NumberFormat() constructor. The Intl.NumberFormat() is a built-in constructor that enables language-sensitive number formatting. Syntax const indianRupee = Intl.NumberFormat(‘en-US’, { style: ‘currency’, currency: ‘currency name’ }); const convertedRupee = indianRupee.format(price); Parameters Locale string – We must pass a locale string we want to convert. Options – The second argument … Read more

How to Compare Two Dates in JavaScript

How to Compare Two Dates in JavaScript

Here are the ways to compare two dates in JavaScript. Using the “getTime()” method Using the “Date()” Object  Method 1: Using the getTime() method You can use the “getTime()” method to convert the Date objects to their numeric representation, which returns the number of milliseconds since January 1, 1970 (Unix Epoch), and then compare the … Read more

How to Convert String to Date in JavaScript [3 Ways]

How to Convert String to Date in Javascript

Here are three ways to convert String to Date in JavaScript. Using the Date() constructor Using Date.parse() method Using toDateString() method Method 1: Using the Date() Constructor The Date constructor parses the string and returns a Date object representing the given date and time string. If the string is recognized as valid, it parses it; … Read more

How to Check If a Function Exists in JavaScript

How to Check If a Function Exists in JavaScript

To check if a function exists in JavaScript, you can use either the “typeof” operator or the “try-catch block”. Method 1: Using the typeof operator The typeof operator checks whether the name of the declared function exists and whether it is a function and not some other type of object or primitive. Syntax if (typeof … Read more

How to Convert a String to Enum in TypeScript

How to Convert a String to Enum in TypeScript

To convert a string to an enum in TypeScript, you can use the “enum’s string values”. Here’s a step-by-step guide to help you achieve this. Step 1: Define the Enum enum MyEnum { ValueA = “VALUE_A”, ValueB = “VALUE_B”, ValueC = “VALUE_C”, } Step 2: Create a function to convert the string to the Enum … Read more

How to Check If String is empty/undefined/null in JavaScript

How to Check If String is empty:undefined:null in JavaScript

To check if a string is empty/undefined/null in JavaScript, you can use the “! operator” that returns true for empty strings, undefined values, null values, and other false values. Example 1 function isEmpty(str) { return !str; } const emptyString = ”; const nullValue = null; const undefinedValue = undefined; const nonEmptyString = ‘Hello, world!’; console.log(isEmpty(emptyString)); console.log(isEmpty(nullValue)); … Read more