Sling Academy
Home/JavaScript/Page 90

JavaScript

JavaScript: Count the occurrences of each word in a string

Updated: Feb 19, 2023
The example below shows you how to count the occurrences of each word in a given sentence in Javascript. The result will be an object where each key is a word, and the value is the number of occurrences of the corresponding word, like......

JavaScript: Set HTML lang attribute programmatically

Updated: Feb 19, 2023
You can dynamically change the lang attribute of your HTML document just by using pure Javascript like this: document.documentElement.setAttribute("lang", 'your language code'); For more clarity, take a look at the example......
JavaScript: Detect a click outside an HTML element

JavaScript: Detect a click outside an HTML element

Updated: Feb 19, 2023
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 click outside an element with the id of my-box: window.onclick = function......

JavaScript: 2 Ways to Get a Random Element from an Array

Updated: Feb 19, 2023
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 a Random Index The key points of this approach......

3 Ways to Shuffle an Array in JavaScript

Updated: Feb 19, 2023
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 and comparing them in the sort()......

3 Ways to Compare 2 Objects in JavaScript

Updated: Feb 19, 2023
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 equal sign) to compare 2 Javascript objects is incorrect......

JavaScript: Checking if an array contains a given object

Updated: Feb 19, 2023
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 (this can be a shallow or a deeply nested object). An object......

JavaScript: Adding a duration (days, hours) to a Date object

Updated: Feb 19, 2023
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 are building a service that allows users to subscribe to a......

How to Write Efficient Loops in JavaScript

Updated: Feb 19, 2023
This article will give you 6 tips to write more efficient loops in Javascript, helping you create faster and more performant apps and websites. Avoid using for-in loops for arrays Using for-in loops for arrays can be slower and can......

Using Labeled Loops in JavaScript (3 Examples)

Updated: Feb 19, 2023
This article is about labeled loops in Javascript. We’ll take a glance at the fundamentals and then walk through a couple of practical examples. Overview In JavaScript, a labeled loop is a loop that has a label, which is a......

JavaScrip Throw Statements: Tutorial & Examples

Updated: Feb 19, 2023
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. When an error occurs in a program, you can use throw to......

Working with Ternary (Conditional) Operators in JavaScript

Updated: Feb 19, 2023
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 operator is: condition ? expression1 :......
← PreviousPage 90 of 90