why the sky isn’t always blue in javascript and why magic is real

a trivial, but interesting way to do boo-lean logic in js

internet sad boy
4 min readFeb 25, 2014

Earlier today I came across a clever little javascript hack (or at least I think so) — the result further validated my addiction to things like the npm registry.

How it happened was random — like most things. I decided to take off my parachute and opened up node’s source code for the first time. I’ve been spending most of my free time trying to understand node so this seemed like a natural next step.

I was drawn to the path module and soon began to reverse engineer the normalize function (I’m specifically talking about the posix implementation).

path.normalize(p) — path core module

The goal of the function is to resolve ‘.’ and ‘..’ elements in the path with array manipulations, remove excessive slashes, and to return a normalized path. The actual documentation for path.normalize is listed below.

The function .normalize() begins by evaluating two boolean expressions, which indicate whether or not the first and last elements in the array equal ‘/’. These boolean values will dictate if a slash should be appended to the front and, or back of the normalized path.

The path is then passed off to the function .normalizeArray() where business logic is handled. The function’s logic makes me excited about the small things — it reminds me that there is always something to learn.

At this point, the .normalize() variable path has been converted from a string into an array of directory names, which is now represented by the local variable parts in .normalizeArray().

*Note: var up = 0.

The for loop iterates through the array by starting at the ending index and decrements for each following value. With the goal of resolving ‘.’ and ‘..’ elements in the path, simple if else if statements and the Array’s .splice() function are used to remove the current index value from the array (how we physically rid ‘.’ or ‘..’).

Notice how in the first else if block the variable up is incremented by 1 after the current index value is removed when (last == ‘..’ ).

The variable up has now changed from 0 to 1. When up = 0, if(up) returns false. When up = 1, if(up) returns true. The reason up is incremented is to record when ‘..’ occurs, and when up has a value greater than zero it passes as a truthy value. This truthy value is then used to remove the directory in front of ‘..’ to simulate movement up the path. The value of up is decremented back to zero in the following iteration(s).

/phantoms/can’t/hang/..

returns

/phantoms/can’t

In traditional boolean logic, 0 indicates false, and 1 indicates true. However, that is not entirely true in this program. I have written thousands of for loops, but what intrigues me about this bit of logic is how they accomplished a typical boolean condition in a way I’d never seen before.

My knee jerk reaction would have been to create a boolean variable and assign a true or false value to it when (last == ‘..’) in the path. I’ve known that passing variables to conditional statements to test for undefined variables is like a thing or whatever, but testing a numerical change without comparing values seems super scripty.

I poked the box and found out that this doesn’t just apply to the typical 0 to 1 boolean true-false relationship (at least in javascriptland). When a variable has a value of 0 or 0.0 it will return false when tested conditionally. When a variable has any value greater than or less than 0 it will return true when tested conditionally. The only value that returns false is zero!

npm install madness -g

- brandan eich

Here are a few examples (test them out if you don’t believe me).

So the sky isn’t always blue, things aren’t always what they seem — that’s what I’m taking away from this; to constantly think of how I can put together different puzzles using the same pieces. The morale of the story is to NOT put lines around things.

p.s. — Life is short, break the rules (javascript does).

--

--