Recently, JavaScript led me down an interesting train of thought: what are the types of data we are dealing with and what shapes do they take in Javascript?

There is an analysis of data I like that is done by using the concept of ‘type’. Now I know from my early days as a programmer that one of the fundamental distinctions of data types in a program is that of a scalar and a non-scalar. In mathematics a scalar is defined as “Any real number, or any quantity that can be measured using a single real number.” The type definition of Scalar in CS is not fundamentally different when we unpack what a ‘single real number’ IS in mathematics. But this article isn’t about the relationship between Type and Sets (like ‘the reals’), that would take many more articles. A scalar in CS is defined as any single value data.

What I came to understand is that while any point of data in JS can be treated as an object and may even have methods that allow us to treat the object as a non-scalar, most primitive data types in JS my be treated as single value and therefore as scalars. These types are: String, Number, Boolean, Null, Undefined, and Symbol. The seventh and final primitive data type in JS, Object, is not a scalar. The interesting thing to me is that since all data in javascript, anything that can be assigned the value of a variable, is an object; the interpretation of any point of data as a scalar is context dependent. E.G.

>s = "scalar"
>for (i=0;i<=s.length;i++){ console.log(s[i]); }



we know:

s[1] === c
s.length === 6

if we examine the properties with the builtin ‘propertyIsEnumerable method, we find:


IMO, from these samples we find that we may treat a string as an array in JS and therefore contextualize a string as not being a scalar. This has many implications meaning we may handle multi-value data as a sting. This is bad in practice… most of the time. It is almost always better to process a string that contains multiple values and store the values in a data-structure that is explicitly non-scalar. This brings me to my main point: While the distinction of any single value as a scalar and fundamentally different from a multiple value data structure like an array seems arbitrary, much of the responsibility of using data structures well is placed on the programmer and as such necessitates the understanding of a scalar as a type of “fundamental particle of data”.

All data in JavaScript is an Object, and all Objects are constructed by associative arrays (hash tables? dictionaries?). This allows us to treat all data in JavaScript as a collection of values. That doesn’t mean we should.

Please feel free to correct me in the comments. If you think it’s stupid that I’m talking about this be sure to tell me why!

Let me leave you with this statement: In JavaScript, it is up to us, the programmers, to define when data is in a scalar like form by treating it contextually as a scalar. This will ultimately help us maximize readability and optimize for maintainability.