Calculate Variance and Standard Deviation in Javascript

November 15, 2021 The Plumber

This short article shows you how to calculate the variance and standard deviation of a given array of numbers. You will see the code in vanilla Javascript and TypeScript. Javascript Output: TypeScript If you use TypeScript…

Javascript: Capitalize the First Letter of Each Word (Title Case)

November 9, 2021 A Goodman

This short article shows you how to capitalize the first letter of each word in a string. hypothetical inputs and expected outputs: we will just capitalize the first letter of a word and keep the rest…

Javascript: Looping through Keys/Values of an Object

September 6, 2021 A Goodman

The examples below show you how to loop through keys/values of a given object in Javascript. Example 1: Using Object.entries() The entries() method returns an array of a given object. It’s supported in all modern browsers…

Javascript: Set HTML lang attribute programmatically

July 21, 2021 A Goodman

You can dynamically change the lang attribute of your HTML document just by using pure Javascript like this: For more clarity, take a look at the example below. Example Preview We are going to build a…

Javascript: Display float with 2 decimal places (3 examples)

July 16, 2021 A Goodman

There are several ways to format a float with 2 (or any number you want) decimal places in Javascript. Example 1: Using toFixed() The code: Output: Example 2: Using toLocaleString() The code: Output: Example 3: Using…

Javascript: 5 ways to create a new array from an old array

April 18, 2020 A Goodman

There are multiple ways to create a true new array from an old array in Javascript (ES6 or beyond). Using the spread syntax (shallow copy) This method works perfectly when you try to clone a one-level…

Merging multiple arrays in ES6

April 18, 2020 A Goodman

In order to merge (or concatenate) two, three or more arrays in ES6, we can use the spread syntax. Example: Output: Note: Using the spread syntax does not change the existing arrays. It always returns a…

[JS] Generate a random number between “min” and “max” and exclude a specific number

February 17, 2020 A Goodman

This article will show you how to generate a random number between a minimum number and a max number and the random number you get will always be different from a given specific number. The code:…

Simple Promise chain example in modern Javascript (ES6+)

February 9, 2020 A Goodman

A simple and easy to understand example of how to use Promise chain in modern Javascript (ES6, ES7, ES8 or higher). Output:

Javascript: Get current date time as 20xx/00/00 00:00:00 format

February 9, 2020 A Goodman

In this tutorial, we will create a new Date object then use the getFullYear(), getMonth(), getDate(), getHours(), getMinutes(), getSeconds() methods to get the current date-time in Javascript. To make sure the month, date, hour, minute, second…