Six ways to declare JavaScript functions

A function is a parametric block of code defined one time and called any number of times later. In JavaScript a function is composed and influenced by many components: JavaScript code that forms the function body The list of function parameters The variables accessible from the outer scope The returned value The context this when the function is invoked Named or anonymous function The variable… Continue reading

How three dots changed JavaScript

When accessing arguments values in a function call, I always felt uncomfortable with arguments object. Its hardcoded name makes difficult to access arguments of an outer function in an inner one (which defines its own arguments). Even worse JavaScript provides it as an array-like object. It is not possible to use array methods like .map() or .forEach() directly on it. To access arguments from the… Continue reading

When 'not' to use arrow functions

It is a pleasure to see the evolution of the programming language you code every day. Learning from mistakes, searching for better implementation, creating new features is what makes the progress from version to version. This is happening to JavaScript these years, when ECMAScript 6 brings the language to a new level of usability: arrow functions, classes and much more. And this is great! One… Continue reading

JavaScript variables hoisting in details

Variables in a program are everywhere. They are small pieces of data and logic that always interact with each other: and this activity makes the application alive. In JavaScript an important aspect of working with variables is hoisting, which defines when a variable is accessible. If you're looking for a detailed description of this aspect, then you're in the right place. Let's begin. 1. Introduction… Continue reading

Gentle explanation of 'this' keyword in JavaScript

1. The mystery of this A lot of time this keyword was a mystery for me and many starting JavaScript developers. It is a powerful feature, but requires efforts to be understood. From a background like Java, PHP or other standard language, this is seen as an instance of the current object in the class method: no more and no less. Mostly, it cannot be… Continue reading

Inheritance in JavaScript: understanding the constructor property

JavaScript has an interesting inheritance mechanism: prototypal. Most of the starting JavaScript developers have hard time understanding it, as well as I had. All types in JavaScript (except the null and undefined values) have a constructor property, which is a part of the inheritance. For example: var num = 150; num.constructor === Number // => true var obj = {}; str.constructor === Object // => true var reg = /\d/g;… Continue reading

Catch the XMLHttpRequest in plain JavaScript

When coding Chrome extensions, sometimes I need to catch the event when JavaScript application starts a XMLHttpRequest (XHR) and finishes. Because the injected script into web page context do not modify the original application, this is hard to do, because pure XHR does not trigger global events. Some libraries, for example jQuery, do trigger global events ajaxStart and ajaxStop, and even offers global ajax events… Continue reading

The magic behind array length property

Developer deals with arrays every day. Being a collection, an important property to query is the number of items: Array.prototype.length. In JavaScript the length does not always indicate the number of existing elements (for sparse arrays) and modifying this property may remove elements. Let's demystify the magic behind this property. Definition The length of an array is an unsigned, 32-bit integer that is… Continue reading

The legend of JavaScript equality operator

JavaScript the Barbarian Sometimes when I'm writing Javascript I want to throw up my hands and say "this is bullshit!" John, JavaScript developer During everyday JavaScript coding, it may be hard to see how the equality operator works. Especially when the operands have different types. Time to time this creates bugs in conditionals, which are difficult to identify. It is easy to understand why 0… Continue reading

JavaScript addition operator in details

Introduction JavaScript is an awesome language. I like it because of the flexibility: just do the things in a way you like: change the variable type, add methods or properties to object on the fly, use operators on different variable types and much more. However the dynamism comes with a price. Developer needs to understand how to handle types conversion for different operators: addition (+), equality… Continue reading