Like building a house.

Just as a builder isn’t expected to manufacture the materials and tools of his trade, a programmer is only concerned with the analogues of ready-made types of materials (primitive data types), basic tools (operations) and the usage instructions (syntax rules) that the programming language provides.

At the most essential level, operations are done on members of data types. These specific instances of data types are called values. And the syntactic beings that operate on values (do the operation) are called operators.

It about time we played with some values of a few types and some operators that manipulate them.

We need values of different types. The rest should follow.

Numbers: Just what they sound like. But not so average and not so run of the mill (the devil is in the details). But for our immediate purposes, they’re just what they sound like! Numbers are definitely useful. They sure are good looking building blocks.

//This is a comment. Lines that begin with // are ignored by JS. //Comments are usually meant for humans. //Things following the '>>' signifies our input. //Things following '>' signifies JS's output.

//Numbers evaluate to themselves. They stand for themselves. //JS isn't doing any work to derive its value. //It's just what it looks like. It's primitive. >>1 >1 >>420 >420

Strings: Strings are sequence of characters flanked by single quotes or double quotes but with the condition that they must be matched. There are some caveats but we’ll cross such bridges when we reach them. (Yes, lot of bridges to cross). Strings signify text. Text is useful right? You could use them for like what a millions things? Sure. So they are yet another primitive data type.

//Strings too evaluate to themselves. >>"How's it going?" >How's it going? >>'She said; "Not too bad." ambivalently.' >She said; "Not too bad." ambivalently. //Wrongo. Unmatched quote. >>'lol" >SyntaxError: unterminated string literal:

We need to operate on our values.

What does + look like to you? That sure is what it does to numbers.

>>1 + 1 >2 >>1 + 2 >3 >>1 + 2 + 3 >6

What if we asked + to operate on Strings? Immaterial of what you thought, JS joins the strings. (String concatenation).

>>"Creativity" + "Lack" >CreativityLack

Now I’m sure you’d be thinking of asking + to operate on a string and a number just for the laugh. Good job bonus points for that. If you didn’t, maybe it’s time you started because if there’s anything that I’d rather you leave with from this is the sense of curiosity. That is essential. Cultivate it if you think you don’t have enough of it.

I’m sure the curious among you are busy in playful discovery. All is very well.

If both the operands (values on which the operator operates on) of the + operator are not numbers, JS converts those values to string and concatenates them.

Aside: This is called operator overloading. The creators of JS decided in an ad hoc manner that + should do arithmetic as well as string concatenation. The conversion of a value to a string is an example of type coercion. (A value is coerced to a value of some other type to satisfy some implied computation)

>> 1 + "shoe" >1shoe >>"1" + '2' //neither are numbers because they're flanked by quotes. >12 >>"1" + 2 + 3 >123 >>1 + 2 + "3" >33 //WT..?

As for the somewhat unexpected turn of events in the last output, let me introduce you to associativity: The rule that resolves order of operation in a sequence of operations.

Consider this, line up a group of your friends facing your, ask them to turn 90 degrees left (their right), spread their arms, hold the respective wrists of the person in front and hug simultaneously. What you get is a left associative order. There are other ways to demonstrate this, but hugging is a sort of association. I think this imagery sticks better. Now if they did it in to the right (their left). We’d have a right associative arrangement.

So you see, + is left associative. In the case of "1" + 2 + 3 , 2 is hugging "1" . 3 is hugging both 2 and "1" . Therefore "1" + 2 evaluates to the string "12" and next operation becomes "12" + 3 which is "123" .

Now I’m sure you see why 1 + 2 + "3" evaluates to "33" . We can override associativity by grouping using parenthesis.

>>1 + (2 + "3") >123

If in doubt, always, make your intention explicit with parenthesis. Tiny bridge we just crossed there. Good job.

Summary.

Playful curiosity is a good thing.

Hug your family, friends and strangers.

REPL: Read Eval Print Loop. <- This

Share this: Twitter

Facebook

Like this: Like Loading...

Tags: associativity, JavaScript, number, operators, primitives, string, values