23
loading...
This website collects cookies to deliver better user experience
==
and ===
, truthy/flasy values, and logical operators. Let's jump in so you can start taking advantage of this javascript magic. ==
or ===
operators. This is a way you can manually control javascript implicit coercion. When you use the ==
operator, javascript will change the data types to match and then compare the two values. When using the ===
Javascript will not change the data types and will compare the values just like they are .let x = 3
let y = ‘3’
x==y //return true
x===y //return false
+
or -
operator. Javascript will behave differently depending on which operator you use. For example, when you add a string and a number together Javascript will automatically convert the number type to a string type and then concatenate (add) the two strings together. When subtracting a string and a number Javascript will change the string data type to a number and then subtract and return a number.let x = 3
let y = ‘3’
x+y //return ’33’
x-y //return 0
0
, 0n
, -0
, “”
, null
, undefined
, and NaN
.let x = 0
let y = “zero”
if(x) {console.log(‘hi’)} // will not console log anything because 0 is a falsy value and converts x to false in the if statement.
if(y){console.log(‘it worked!’)} // y is a truthy value. Y is converted to true in the if statement.
let x = 0
let y = ‘zero’
x||y //returns ‘zero’ first value is false so will return the second value
y||x //returns ‘zero’ first value is truth so will return first value
&&
operator javascript will check to see if any of the values are falsy. If any value is falsy javascript will return the first falsy value. If both values are true then javascript will return the second value.let x = 0
let y = ‘zero’
let z = undefined
let a = ‘hello’
x&&z //returns 0. 0 is falsy so javascript returns the first falsy value.
y&&z //returns undefined. z is false so javascript returns the first falsy value.
y&&a //returns ‘hello’. both values are truthy so javascript returns the second value.