Boolean Logic
True, False, and Beyond
Boolean Logic
•Everything starts with the idea
that a statement is either True or
False
•Then we can combine those
initial statements to create more
complex statements that also
evaluate to True or False
Comparison Operators
Assuming x = 5
Equality Operators
== vs. ===
var x = 99;
x == "99" //true
x === "99" //false
var y = null;
y == undefined //true
y === undefined //false
"==" performs type coercion, while "===" does not
Logical Operators
AND, OR, and NOT
Assuming x = 5 and y = 9
Exercise 1
var x = 10;
var y = "a"
y === "b" || x >= 10
Exercise 2
var x = 3;
var y = 8;
!(x == "3" || x === y) && !(y != 8 && x <= y)
Exercise 3
var str = ""
var msg = “lol!"
var isFunny = "false"
!(( str || msg ) && isFunny)
Type Conversion
•In JavaScript there are 5 different data types that
can contain values:
• string
• number
• boolean
• object
• function
Number() converts to a Number,
String() converts to a String,
Boolean() converts to a Boolean.
Example
var str = "123";
Number(str) // returns a number from a string variable str ie, 123
var x = 5;
String(x) // returns a string from a number variable x ie, "5"
String(true) // returns "true"
Boolean(0) // returns false
Boolean(12) // returns true
Boolean("") // returns false
Type Coercion
•Type Coercion refers to the process of automatic
or implicit conversion of values from one data type
to another.
•This includes conversion from Number to String,
String to Number, Boolean to Number etc. when
different types of operators are applied to the
values.
Example
// The Number 10 is converted to
// string '10' and then '+'
// concatenates both strings
var x = 10 + '20';
var y = '20' + 10;
// The Boolean value true is converted
// to string 'true' and then '+'
// concatenates both the strings
var z = true + '10';