JavaScrip Throw Statements: Tutorial & Examples
This practical article shows you how to use throw statements to deal with errors in Javascript. Overview In JavaScript, the throw statement is used to throw an exception….
Working with Ternary (Conditional) Operators in JavaScript
This article is about ternary operators (or conditional operators) in Javascript. The Basics Ternary operators are a shorthand way of writing if-else statements. The syntax for a ternary…
JavaScript: Adding a duration (days, hours) to a Date object
This article shows you a couple of different ways to add a duration (days, hours, minutes, seconds) to a Date object in Javascript. Using getTime() method Imagine you…
JavaScript: Checking if an array contains a given object
In JavaScript, arrays are commonly used to store a collection of values. There might be scenarios where you want to check whether an array contains a given object…
3 Ways to Compare 2 Objects in JavaScript
This article walks you through 3 different ways to compare 2 objects in Javascript. Without any further ado, let’s get started. Warning: Using the === operator (the triple…
3 Ways to Shuffle an Array in JavaScript
This practical article walks you through 3 different approaches to shuffling a given array in Javascript. Using Sort() Function You can shuffle an array by generating random numbers…
JavaScript: 2 Ways to Get a Random Element from an Array
This concise, code-centric article shows you 2 different ways to get a random element from a given array in Javascript. Without any further ado, let’s get started. Creating…
JavaScript: Detect a click outside an HTML element
With Javascript, you can easily detect if the user clicks somewhere outside a given HTML element. For example, the code snippet below shows you how to catch a…
Calculate Variance and Standard Deviation in JavaScript
This practical, succinct 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…
JavaScript: Set HTML lang attribute programmatically
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….