How well do you know JavaScript Arrays?

I decided to write about sparse and dense arrays several months ago. I thought it would be easy and imagined writing a masterpiece based off my multi-year experience with arrays. Alas, my foray into the intricacies of Arrays unearthed surprising discoveries and shattered my brittle expertise. This series of posts describes my learnings and Aha moments.

Sorting in JavaScript

What does the JavaScript snippet below return? If you think it would be [1, 2, 3, 10, 20, 30], you are in for a big surprise! Why? The default sort in JavaScript is the lexicographic (alphabetical) sort. All operands are cast into strings before comparison; see this section of the JavaScript spec. It is possible to override this … Continue reading Sorting in JavaScript

Understanding JavaScript Array methods: Unshift and Shift

Shift and Unshift are the counterparts of Push and Pop. This post provides a deep dive into their applications. Unshift Unshift, a unary function, 'shifts' the array to the right and inserts its parameters into the first slots. It then returns the new length of the array. It is the complement of push which appends values … Continue reading Understanding JavaScript Array methods: Unshift and Shift

JavaScript Array Deep Dive: Push and Pop

Push and Pop are two popular methods for handling arrays in JavaScript; this short post explores both array methods. Push Push is used to add elements to the end of an array and returns the array's new length. Pushing a new element into an empty array will return a value of 1 (the new length of the … Continue reading JavaScript Array Deep Dive: Push and Pop

What you didn’t know about JSON.parse

Now that you know some more about JSON.stringify; let's dive into its complement: JSON.parse. 1. JSON.Parse The JSON parse function takes in a string (invalid JSON will cause a SyntaxError exception). If parsing succeeds, JSON.parse returns the corresponding value or object. 2. The reviver function JSON.parse accepts an optional reviver function. This reviver function, if specified, allows you … Continue reading What you didn’t know about JSON.parse

Nope, You don’t need lodash for that

I recently had to reduce the size of an Angular Web app for performance reasons. A quick run through the webpack bundle analyzer identified MomentJS and Lodash as the main culprits. Consequently, I had to eliminate both libraries and implement replacements in pure ES6. The upside however is that it was easier than I thought! Thus, … Continue reading Nope, You don’t need lodash for that

Working with Integers in JavaScript

One of the ways JavaScript differs from most programming languages is the absence of integer types. Every numeric value is a Number which is based off the IEEE754 floating point representation. You can still have 'integer' values and it is technically possible to approximate this behaviour using UInt*Array types but that's a discussion for another day. This … Continue reading Working with Integers in JavaScript

Why you should not use isNaN in JavaScript

I was on a video streaming site recently and moved the play point to the far right. It was amusing to see the hover details show NaN:NaN - ahah, some mathematical operation had NaN-ed and the code didn't cater for that. If you have read Why JavaScript ‘seems’ to get addition wrong; you would have seen … Continue reading Why you should not use isNaN in JavaScript

Faking goto in JavaScript

What if I told you JavaScript had a limited form of the infamous goto statement? Surprised? Read on. Labeled Statements It is possible to add label identifiers to JavaScript statements and then use these identifiers with the break and continue statements to manage program flow. While it might be better to use functions instead of labels to jump around, it … Continue reading Faking goto in JavaScript

What you didn’t know about JSON.Stringify

JSON, the ubiquitous data format that has become second nature to engineers all over the world. This post shows you how to achieve much more with JavaScript's native JSON.Stringify method. A quick refresher about JSON and JavaScript: Not all valid JSON is valid JavaScript JSON is a text-only format, no blobs please Numbers are only base … Continue reading What you didn’t know about JSON.Stringify

JavaScript has no Else If

Surprised? Read on. The following code might be very familiar to you, in fact I write this too a lot. However what JavaScript actually parses is shown below Same end result but closer to the underlying language semantics. Why? JavaScript allows if/else conditionals to omit the wrapping braces - some actually argue this is a neater style … Continue reading JavaScript has no Else If

Why JavaScript has two zeros: -0 and +0

Do you know there are two valid zero representations in JavaScript? In pure mathematics, zero means nothing and its sign doesn't matter. +0 = -0 = 0. Computers can't represent value well enough and mostly use the IEEE 754 standard. Most languages have two zeros! The IEEE 754 standard for floating point numbers allows for … Continue reading Why JavaScript has two zeros: -0 and +0

Understanding JavaScript Property Descriptors 3

If this is your first time here, you should read the part 1 and part 2 of this series. Then come back to this to continue. Now that we know the basics, this post covers the JavaScript methods for setting and modifying object property descriptors. 1. Object.preventExtensions() This blocks the addition of new properties to an object. Literally, … Continue reading Understanding JavaScript Property Descriptors 3

Understanding JavaScript Property Descriptors 2

If this is your first time here, you should read the first post in this series. Then come back to this to continue. Continuing with the dive into property descriptors, this post goes deeply into the properties, what they mean and how they can be used. 1. Modifying existing properties The defineProperty method allows users to create and modify … Continue reading Understanding JavaScript Property Descriptors 2

Deep dive into JavaScript Property Descriptors

Creating Object Properties There are a couple of ways to assign properties to objects in JavaScript. The most common example is using obj.field = value or obj['field'] = value. This approach is simple however, it is not flexible because it automatically defines property descriptor fields In all 4 obj objects, the foo property returns the same result. But are they … Continue reading Deep dive into JavaScript Property Descriptors

Why I am moving to Angular 2

I started poking into core Angular 2 concepts a few weeks ago and it has been a pleasant experience so far. I rewrote a bare-bones replica of an Angular 1 app that took me months in about 2 or 3 weeks. Although rewrites are typically faster due to familiarity, it was impressive seeing built-in support for … Continue reading Why I am moving to Angular 2

Book Review:Build your own AngularJS

As part of my continuous learning; I started reading Tero Parviainen's 'Build your own AngularJS' about 6 months ago. After 6 months and 127 commits, I am grateful I completed the book. While I didn't take notes while reading, some ideas stood out. Thus, this post describes some of the concepts I have picked up from the book. The Good 1. Get … Continue reading Book Review:Build your own AngularJS

How to track errors in JavaScript Web applications

Your wonderful one-of-a-kind web application just had a successful launch and your user base is rapidly growing. To keep your customers satisfied, you have to know what issues they face and address those as fast as possible. One way to do that could be being reactive and waiting for them to call in - however, … Continue reading How to track errors in JavaScript Web applications

How function spies work in JavaScript

If you write unit tests, then you likely use a testing framework and might have come across spies. If you don't write unit tests, please take a quick pause and promise yourself to always write tests. Testing framework suggestions? Try Sinon or Jasmine. Spies allow you to monitor a function; they expose options to track invocation counts, … Continue reading How function spies work in JavaScript

Learning ES2015 : let, const and var

Lions at the zoo Zoos allow for safely viewing dangerous wild animals like lions. Lions are caged in their enclosures and can't escape its boundaries (if they did, it'd be chaos eh?). Handlers, however, can get into cages and interact with them. Like cages, you can think of variable scoping rules as establishing the boundaries and walls in … Continue reading Learning ES2015 : let, const and var

Learning ES2015 : Getting Started

ES6 (also ES2015) is the rave of the moment. Finally JavaScript is getting a makeover after nearly 6 years. The enhancements allow for more powerful and expressive JavaScript, ease the building of complex applications and iron out some quirky behaviour. The var hoisting problem? let resolves that. IIFEs? The function context syntax eliminates the need for that. There's … Continue reading Learning ES2015 : Getting Started