Posts

Showing posts with the label advancedjs

Categorizing values in JavaScript

This post examines four ways in which values can be categorized in JavaScript: via the hidden property [[Class]], via the typeof operator, via the instanceof operator and via the function Array.isArray() . We’ll also look at the prototype objects of built-in constructors, which produce unexpected categorization results. [This post is a copy of my Adobe Developer Connection article , I’m publishing it here for archival purposes.] Required knowledge Before we can get started with the actual topic, we have to review some required knowledge. Primitives versus objects All values in JavaScript are either primitives or objects. Primitives. The following values are primitive: undefined null Booleans Numbers Strings Primitives are immutable, you can’t add properties to them: > var str = "abc"; > str.foo = 123; // try to add property "foo" 123 > str.foo // no change undefined And primitives are compared by value , they are ...