Practical Boolean Example in JavaScript

Textbooks love this. You read about how in programming, Booleans determine either TRUE versus FALSE and you may observe a bunch of different scenarios in order to help make the concept more clear. Ok, that makes sense, but I’m a big fan of learning that actually does something. So here is a more specific programming example.

Function Error Catching

Programming often requires imagining every possible situation for something and this example is no different. Imagine you are writing a program that makes change. Let’s just pretend the actual brainwork of making the change happens in a function.

funcbool1

So far, everything looks good, but now consider something else:

// What if they didn’t give any money?

funcbool2

The Solution: Boolean Conversion

So far, that works fine, but every time the program runs, it has to pause and think about something. What if there was a sort of, “express lane” for this cash handling? A real life comparison would be when a customer steps up to the cash register with exact change, and really quite honestly – there is no need to even consider the math for that scenario, right? For computers, while they work fast with the simplest of questions – cutting back on those moments where a program is stopping to consider something briefly – is where more complex tasks impact processing times and can and should be minimized.

In the example below, the best solution to handling that “what if” moment is through Boolean conversion. Doing it that way is useful because it is both verbose and the program will run FAST. It will immediately find true and cut out any extra steps. When things work normally as they are expected to behave, it will take the first true part of the Boolean and ignore the rest.

funcbool3

Boolean ‘Gotchas’

So now that you know how to use Boolean in an efficient manner, watch out for the less obvious Boolean scenarios that can cause problems. In JavaScript, type conversion exists and working on something like adding 7 + 7 + “7” will be considered completely ok and run correctly. But don’t let that catch you off guard. If the Boolean compares a string, a string will still be true! For example, “0” and “false” at first glance look like they would be false. But no, they would return as true because quotation marks around anything will make it into a STRING.

Speaking of Speed…

JavaScript is well-loved for being fast. But one quirky thing it has is the requirement of arranging the curly braces in a very specific manner. The position of the braces do matter a great deal, so always abide by this rule:

“Always put the start curly brace at the end of a line, and not a new line.”

For example, at first, you might think these two functions below look the same, but:

funcbool4

The first function not work. But, why not? Syntactically, the parentheses both even close, right?!

The real reason for this bizarre behavior is because JavaScript is very efficient, but also somewhat “simple minded”. Think of how JavaScript reads programming code as much like leading a horse with a carrot tied to a string. The animal may not look ahead, but instead merely follow what it sees immediately in front, blindly stepping forward with little regard to what is happening. All that the horse thinks is “carrot, carrot, carrot…”. Look again at that part where return happens:

funcbool5

When return happens, JavaScript drops out of the function immediately. Going back to the carrot on a string comparison, there is no awareness. JavaScript sees return and return means LEAVE THIS BLOCK OF CODE*. Parentheses opening and then closing in this case is not going to stop that decision on JavaScript’s part. Think of the horse eating the carrot: it keeps taking big bites out of the carrot anyway as he follows what is in front of him. Until the carrot is yanked away entirely, the horse will continue to step forward. Therefore to remedy, keep leading on JavaScript with an open brace on the same line to let it know there is something extra attached here.

funcbool6

*Or instead of shouting LEAVE, one could quote Dr. Chuck from his Programming For Everybody class (my personal favorite teacher ever, for programming), and see it as the JavaScript essentially goes ahead and immediately asks “what’s next?” – and very enthusiastically in this case.

Advertisements

About Juliette

Techy girl who also loves both technology and culinary arts. Programming and recipes are my thing! Currently learning about web development while taking a bite out of a delicious crusty piece of bread.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s