Latest Javascript Tutorials and How-To's


Flatten Array Containing Arrays in JavaScript

Flattening an array refers to the process taking an input array, which may itself contain n-dimensional arrays as its elements, to a single array. This can be done in Javascript using the flat() method.
December 19, 2019

Format Number as Currency in Javascript

The Intl.NumberFormat object can be used to format a number as a currency using Javascript. Various options can be set to customize format of currency code, comma / decimal separators etc.
December 18, 2019

Knowing whether Promise is Completed (either Fulfilled or Rejected) with finally() Callback

The finally() callback can be used to know whether the Promise was settled. We don't care whether it was resolved or rejected.
December 17, 2019

Setting Default Values to Variables if null or undefined (using ES2020 Nullish Coalescing Operator)

The nullish coalescing operator has been newly introduced in Javascript. This can be used to give default values if a variable is either null or undefined.
December 16, 2019

What Happens to setTimeout() / setInterval() Timers Running on Inactive Browser Tabs ?

Time delay for setTimeout() / setInterval() methods executing on inactive browser tabs are set to one second regardless of their value defined in code.
December 15, 2019

How to Disable Page Scrolling when Modal Dialog is Opened

When a fixed-position modal dialog is opened, scrollbars on the page can be removed by dynamically setting overflow:hidden CSS for the 〈body〉tag.
December 14, 2019

Preventing Addition of New Properties to a Javascript Object with Object.seal()

The Object.seal() method seals an object. After an object gets sealed, no new properties can be added to it.
December 11, 2019

Preventing Changes to a Javascript Object with Object.freeze()

The Object.freeze() method can make an object or array immutable. No changes can be made to that object once it gets immutable.
December 10, 2019

Hashbang Comments - The Third Way of Adding Comments in Javascript Code

Hashbang comments is the third type of syntax for adding comments to Javascript code. This comment can specify the Javascript interpreter that is to be used to execute the code within a script file or a module.
December 8, 2019

Loading CSS Stylesheet on Demand Through Its disabled Attribute

A stylesheet can include the disabled attribute in its markup to prevent it from being downloaded on page load. Later when the stylesheet needs to loaded on demand, its disabled attribute can be removed with Javascript.
December 3, 2019

Numeric Separators in Javascript to Improve Readability of Large Numbers

Numeric separators can be used to improve readability of large numbers. For example the number 111111111111111111111111, which is difficult to read, can be alternately written as 111_111_111_111_111_111_111_111
December 2, 2019

Detecting Change in Playing Subtitles Text for 〈video〉Element

Sometimes we would like to detect when the subtitles change in the playing video. This can be done by listening to the cuechange event on the 〈track〉element.
November 29, 2019

Preserving Context of this Inside AJAX Response Callbacks in jQuery

It is very common to access current this context inside jQuery AJAX's response callbacks (success or error). This tutorial describes 3 ways of doing this.
November 28, 2019

Checking whether Page is Opened / Active in Browser Tab

The Page Visibility API can be used to check whether a browser tab is active or not, i.e. if the page is in view or hidden from view.
November 25, 2019

How to Load Google Fonts with JavaScript

Google Fonts can be loaded through Javascript using the Web Font Loader Library. Events are also triggered when fonts are being loaded, or when they are rendered.
November 18, 2019

Converting Timestamp to Date & Time in Javascript

A UNIX timestamp can be converted to the required date and time formats in Javascript using the Date() object and its methods.
November 17, 2019

Looping through querySelectorAll() with Javascript

List of nodes returned by querySelectorAll() can be looped through using the forEach() method of the returned NodeList object.
November 16, 2019

Differences between 〈script type=module〉and〈script〉

Whenever a Javascript module needs to be applied to HTML, we need to do it within a 〈script type=module〉 tag. However there are some difference in Javascript execution in this tag vs a normal 〈script〉tag.
November 15, 2019

How to Loop through〈select〉Options with Javascript

Options of a dropdown select element can be looped over its options property. This property gives a list of all option elements inside it.
November 13, 2019

What is script type=module ?

A script tag having type=module attribute specifies that it to be considered as a Javascript module. It may be importing other Javascript module(s) inside it and becomes a top-level module for the imported modules.
November 10, 2019