Today I learned...
Boolean
- true or false values
- values can also be a result of comparisons, ex.
let isGreater = 5 > 2; \\true
null value
- a special value that represents "nothing", "empty", or "value unknown"
undefined value
- represents a value not assigned
- can explicitly assign
undefinedvalue to a variable but this is not recommended. Instead, use thenullto assigning a "nothing" or an "empty" value.
Objects
- object type is special
- not a primitive value
- represents more complex data structures
Symbols
- used to create unique identifiers for objects
typeof operator
- returns the type of argument
-
typeof xis equivalent totypeof(x) -
typeof nullreturns object which is incorrect and an error of JS -
typeof alertwill return function but functions are technically an object data type
Conditionals
String Comparison
- strings are compared letter by letter
- longer strings are greater
- uses Unicode
Comparison of Different Types
- converts values to numbers
Comparison with null and undefined
-
null===undefinedis false (strict equality) -
null==undefinedis true (loose equality)
For math and comparisons (<,>,>=,<=)
-
nullbecomes 0 -
undefinedbecomesNaN.
Strange Result: null vs 0
-
null > 0is false, null becomes 0 with the comparison operator, 0 is not greater than 0. -
null == 0is false, the == equality does not convert null and null does not equal 0. -
null >= 0is true, null is converted to 0 and fulfills one of the operator condition (0 equals 0)
An incomparable undefined
- shouldn't be compared to other values
-
undefined > 0is false -
undefined < 0is false, examples 1 and 2 show undefined converted to NaN by the comparison operators and NaN is a numeric value and returns false for all comparisons. -
undefined == 0is false, returns false because the undefined only equalsundefinedornull.
Treat comparisons with undefined\null with care.
Resources
The Odin Project
https://javascript.info/types
https://javascript.info/comparison
Top comments (0)