Learning to Code: Day 44 — Basic JavaScript Part 9

Hugh Burgess
4 min readJan 4, 2021

Hello everyone! Hope you all had a great new year, it’s been a while since I had my head in the JavaScript mindset, last time we left off at queues, so let’s pick up from there, starting with Booleans.. Thanks as always to FreeCodeCamp for the lessons.

Boolean Values

So what are Booleans? They are data types which have only one of two possible values: true or false. Imagine them like little “switches” where True is “on” and False is “off”.

Note: The above quotes used are not included when writing true or false in JavaScript, as they are not Strings.

Conditional Logic with If Statements

When you are writing code, the layout and execution is very logical. What works for what and how does this affect that. As a web developer you would need to think out the steps to execute an action and so on. Here we are getting into just that.

An If statement are used when we need to make decisions when writing code. It tells JS to execute the code in the curly brackets {} if the conditions in the parenthesis () are met. These conditions are Boolean conditions as we saw above, and may only be true or false. For example:

- FreeCodeCamp

When writing an If statement with Boolean conditions that may also not be met, we need to write the return statement for which it would result as false. This is like saying “If … then return true, otherwise return false”.

- FreeCodeCamp

Remember: As you can see in the above example, the Boolean condition myCondition is a very simple condition, and could be far more complex later on, but for now we are starting out very simple. Also note the positioning of the return false statement inside the curly brackets of the function but outside of the if statement curly brackets.

Comparison with the Equality Operator

All comparison operators in JS return a value of either true or false. The most basic equality operator being “==”. It compares two values and returns true if they are equal or false if otherwise.

-FreeCodeCamp

Here, we can see many similarities to the above chapter example, except now our Boolean condition includes an equality operator, so as to say “if this equals that, then..”, in this case, if the Boolean condition myVal is equal to 10.

Note: Listen up, this is a biggie: In order for JS to compare to differing data types such as strings and numbers, it must convert one to another, also known as “Type Coercion”. This goes as follows:

-FreeCodeCamp

The Strict Equality Operator

Let’s take this one step further and look at the strict equality operator, written as “===”. It is a counterpart to the equality operator, and does not perform a type conversion.

Where the equality operator would say that one data type can be compared to another, that this equals that, the strict equality operator says that only this equals this and not that.

Yes, I didn’t make that very clear - in plain English, it is saying that the differing data types are unequal and will return false.

-FreeCodeCamp

Remember: In summary, the equality operator “==” performs a type conversion between differing data types and evaluates the values. The strict equality operator “===” looks at the differing data types as they are, and type conversion is not performed.

Note: You can use the typeof operator to define a variable or value by it’s data type category.

Aaaaand let’s leave it there for today before we really start to get into the thick of these operators, we’ll dig into them more next time. Til then!

--

--