DEV Community

Jennifer Tieu
Jennifer Tieu

Posted on • Updated on

Self-Taught Developer Journal, Day 27: TOP JavaScript Fundamentals Part 2 - Data Types Overview cont. and Comparisons

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 undefined value to a variable but this is not recommended. Instead, use the null to 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 x is equivalent to typeof(x)
  • typeof null returns object which is incorrect and an error of JS
  • typeof alert will 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 === undefined is false (strict equality)
  • null == undefined is true (loose equality)

For math and comparisons (<,>,>=,<=)

  • null becomes 0
  • undefined becomes NaN.

Strange Result: null vs 0

  1. null > 0 is false, null becomes 0 with the comparison operator, 0 is not greater than 0.
  2. null == 0 is false, the == equality does not convert null and null does not equal 0.
  3. null >= 0 is 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 > 0 is false
  • undefined < 0 is 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 == 0 is false, returns false because the undefined only equals undefined or null.

Treat comparisons with undefined\null with care.

Resources

The Odin Project
https://javascript.info/types
https://javascript.info/comparison

Top comments (0)