Skip to main content

Values, Types, and Operators 10


A computer stores bits - 0s, 1s. You need to convert your JS code in the background into those bits so that the computer can understand it.

Values 10


  • Every value has a type
  • a value is a name that represents a place in the memory
  • to create a value, invoke its name let x = 5

Types:

  • numbers
  • strings
  • booleans
  • logical operators
  • Empty values

Operators:

  • +,-,*,/
  • modulo - remainder of division

Numbers 11


  • 64 bits to store a number value
  • 2^64 possibilities

Special Numbers

  • infinity
  • -infinity
  • NaN - not a number, but still a type of Number. You get this when your operation doesn't yield a meaningful result
    • "dog" + 5 = NaN

Strings 13


  • represents text
  • a series of bits - unicode - 16 bits per string element
  • try not to mix and match quoting styles
  • backticks are cool. Use them a lot. These are called template literals
  • sometimes you need to escape a character like a backslash.
  • "con" + "cat" + "e" + "nate" joins them to become "concatenate"
  • half of 100 is ${100 / 2} is an example.

Unary operators 15


  • typeof

Boolean values 15


  • evaluate to true or false
  • used with comparison operators: if (x === y), do these steps.
  • strings can also be used in comparisons - console.log("Aardvark" < "Zoroaster")
  • console.log(NaN == NaN) is false

Logical Operators


  • and, or, not - && || !
  • manipulate the boolean values
  • not operator flips the value of the operation: !true equals false
  • precedence: || -> && -> comparison operators

Empty values 17


  • null and undefined - these are very similar but not quite.

Automatic type conversion 18


  • null converts to zeo
  • `"4" + 3 -> equals a number 7
  • will take the result and do the best JS can to define what type the output is. This is called type coercion.

Short Circuiting of Logical Operators


The || operator will return the value to its left when the left is true and wiill return the value on its right if it is false The && operator is the opposite.

console.log(null || "user")
// → user
console.log("Agnes" || "user")
// → Agnes

true || X and false && X will never be evaluated to X

Summary 20


We looked at four types of JavaScript values in this chapter: numbers, strings, Booleans, and undefined values. Such values are created by typing in their name (true, null) or value (13, "abc").

You can combine and transform values with operators. We saw binary operators for arithmetic (+, -, *, /, and %), string concatenation (+), comparison (==, !=, ===, !==, GT, >, GT=, >=), and logic (&&, ||, ??), as well as several unary operators (- to negate a number, ! to negate logically, and typeof to find a value’s type) and a ternary operator (?:) to pick one of two values based on a third value.