JavaScript: Literal Initialiser [] vs Constructor new Array() for Creating Arrays

The [] method is an array literal/array shorthand to initialise the array. The syntax is simple: a comma-separated list of values in square brackets []

The new Array() method is to create an instance by calling the array constructor. The new Array() syntax has three different forms: If you call the constructor with TWO […]

Continue reading


JavaScript: Alternative method for HTML 5 Data Attribute in Old Browsers

The HTML5 datalist property is not available in older browsers (it can be polyfilled easily enough though). The standard getAttribute method can be used instead of course, and data-xxx attributes on HTML elements are accepted by all browsers, IE also has supported this method from IE4. Sample HTML

JavaScript

Continue reading


JavaScript: Convert NodeList to Array

NodeLists are very similar to Array collections of elements, often referred to as “array-like”, but don’t feature many of the methods provided by the true Array, like forEach, map, filter, etc. JavaScript does, The result of converting NodeList to Array below is a true Array object containing all of the nodes returned 1. Array.prototype.slice.call

[…]

Continue reading


JavaScript: The Execution Order of DOMContentLoaded and Load (window & document)

DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading Load event is fired when page finished loading or loaded completely. According to Parsing HTML documents – The end, The browser parses the HTML source and runs deferred scripts. A […]

Continue reading