Member-only story
Object-Oriented JavaScript — Comparisons and New Primitive Types
JavaScript is partly an object-oriented language.
To learn JavaScript, we got to learn the object-oriented parts of JavaScript.
In this article, we’ll look at boolean, comparisons, and new primitive types which are the building blocks of objects.
Lazy Evaluation
Booleans expressions are lazily evaluated.
This means that it evaluates the expression until the result is clear.
So if we have:
true || "foo";
then the JavaScript engine stops at true
and returns that because it’s clearly true
no matter what the 2nd operand is.
However, if we have:
true && "foo";
then both are evaluated and the 2nd operand is returned.
We can use this behavior to let us initialize variables to a default value.
For instance, we can write:
let num = num || 10;
If num
is falsy, then num
will be assigned 10.
Comparison
Comparison operators also return boolean values.