JavaScript: How to Join (Merge) Arrays

JavaScript: How to Join (Merge) Arrays

Best Solution: Using Array.concat() The Array.concat() method in JavaScript is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. Here’s an example of how to use concat() to join two arrays: In the code above, concat() merges array1 and array2 into a new…

JavaScript: How to filter Array of Objects by Property

JavaScript: How to filter Array of Objects by Property

Best Solution: Using Array.filter() This is the most common method used to filter an array of objects. It creates a new array with all elements that pass the test implemented by the provided function. In this code, we use the filter() method to create a new array highScoringStudents containing only the students who scored more…

JavaScript: How to remove Last Character from String

JavaScript: How to remove Last Character from String

This post is a special case of removing a character from a string in JavaScript. If you want a more general explanation about how to manipulate strings, head to that post instead. But if you’re looking specifically to remove the last character from a string, keep reading. Best Solution: Using slice() The slice() method extracts…

JavaScript: Convert & Format Date object to a String

JavaScript: Convert & Format Date object to a String

Best Solution: Using toISOString() The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ. The timezone is always zero UTC offset. Using toString() The toString() method converts a Date object to a string in the following format: Day Month Date Year Time (Time Zone). Custom…

JavaScript: Get Unique values from Array

JavaScript: Get Unique values from Array

Best Solution: Using a Set In this code snippet, new Set(array) creates a new Set object from our array, automatically removing any duplicates. The spread operator (…) then converts this Set object back into an array. Remember that the Set object does not guarantee to maintain the original order of elements. If you need to…

JavaScript toFixed() Function: A Comprehensive Guide

JavaScript toFixed() Function: A Comprehensive Guide

The toFixed() function in JavaScript is a method of the Number prototype. It formats a number using fixed-point notation, returning the result as a string. The function accepts one argument: the number of digits to appear after the decimal point. In the above example, the toFixed() function rounds the number to two decimal places. Remember,…

JavaScript: Round to 2 decimal places

JavaScript: Round to 2 decimal places

The Problem You want to round a number with n decimal places to a number with only 2 decimal places. And maybe you want to do it only if necessary, so when you have a number with less than 2 decimal places, you don’t want to add extra zeros. Example Input: Output: Best Solution: Using…

JavaScript Event Bubbling Simplified

JavaScript Event Bubbling Simplified

What is JavaScript Event Bubbling? JavaScript Event Bubbling is a method of event propagation through the elements of the Document Object Model (DOM). When an event, such as a click or mouseover, occurs on a specific element, that event is not only triggered on that element but also on all its parent elements. This is…

The Complete List of JavaScript Event Listeners

The Complete List of JavaScript Event Listeners

Event listeners in JavaScript are like alert guards, standing by and waiting patiently for a specific action to happen on a particular element. This action could be anything – a mouse click on a button, a gentle hover over an element, the complete loading of a webpage, or even a simple stroke on your keyboard.Once…

JavaScript Object Prototypes Explained

JavaScript Object Prototypes Explained

What is the Object Prototype in JavaScript? Simply put, object prototypes are Javascript’s way of representing inheritance between objects. The prototype of an object (usually defined by the __proto__ property) is a reference to its parent object and it holds all the parent’s features that this object has inherited. This inheritance allows objects to access…