Eloquent JavaScript - Chapter 1 - Values, Types, and Operators (Notes)

bit-sea

Eloquent JavaScript(The Annotated Version) - Chapter 1 - Values, Types, and Operators

JavaScript Value Types

  • Numbers
  • Strings
  • Booleans
  • Undefined
  • Null

  • Objects

  • Functions
  • Arrays

Numbers

  • 64-bit floating point
  • IEEE-754(aka “Double”)

  • 264 different numbers which is about 18 quintillion (an 18 with 18 zeros after it).

    The actual maximum whole number that can be stored is more in the range of 9 quadrillion (15 zeros), which is still pleasantly huge.

    Not all whole numbers below 18 quintillion fit in a JavaScript number, though.

    Those bits also store negative numbers, so one bit indicates the sign of the number.

    To do this, some of the bits are used to store the position of the decimal point.

Arithmetic
  • The % symbol is used to represent the “Remainder“ operation, not “modulo“.
Special numbers
  • Infinity - positive infinity
  • -Infinity - negative infinity
  • NaN - “not a number” which is a value of the number type.
    1. Result of undefined or erroneous operations
    2. Toxic: any arithmetic operation with NaN as an input will have NaN as a result.
    3. Nan is not equal to anything, including NaN
      console.log(NaN == NaN)
      // → false

NaN is supposed to denote the result of a nonsensical computation, and as such, it isn’t equal to the result of any other nonsensical computations.

Strings - anything in ‘’ or “”

Backslash () - “escaping“ the character.

e.g.
\”, \n, \t means a tab character.

Unary operators

Not all operators are symbols. Some are written as words.

e.g.
the typeof operator (produces a string value naming the type of the value)

console.log(typeof 4.5)
// → number
console.log(typeof "x")
// → string
  • Sequence of 0 or more 16-bit characters
    • UCS-2, not quite UTF-16
    • No awareness of surrogate pairs
  • No separate character type
    • Characters are represented as strings with a length of 1
  • Strings are immutable
  • Similar strings are equal (==)
  • String literals can use single or double quotes

Boolean - true or false

Comparisons

String comparison is based on the Unicode standard.
This standard assigns a number to virtually every character you would ever need.
When comparing strings, JavaScript goes over them from left to right, comparing the numeric codes of the characters one by one.

Logical operators
  • The && operator represents logical and. It is a binary operator, and its result is true only if both the values given to it are true.

  • The || operator denotes logical or. It produces true if either of the values given to it is true.

  • Not is written as an exclamation mark (!).

  • || has the lowest precedence, then comes &&, then the comparison operators (>, ==, and so on), and then the rest.

  • Ternary operator, operating on three values. It is written with a question mark and a colon, like this:

console.log(true ? 1 : 2);
// → 1
console.log(false ? 1 : 2);
// → 2

The value on the left of the question mark “picks” which of the other two values will come out.

When it is true, the middle value is chosen, and when it is false, the value on the right comes out.

Undefined values

  • Null and Undefined, that are used to denote the absence of a meaningful value.

  • Many operations in the language that don’t produce a meaningful value yield undefined simply because they have to yield some value.

  • Treating Undefined and Null as interchangeable.

Automatic type conversion

console.log(8 * null)
// → 0
console.log("5" - 1)
// → 4
console.log("5" + 1)
// → 51
console.log("five" * 2)
// → NaN
console.log(false == 0)
// → true

When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it wants, using a set of rules that often aren’t what you want or expect. This is called type coercion.

  1. Null on either side of Arithmetic will convert to be 0 if numeric on the other side.
  2. String can be convert to be number alongside of “-“ convert to be number.
  3. if Strings on either side of “+”, numeric values will be convert to be string and concatenated.
  4. (Strings that can’t be convert to numeric , false, null, undefined) “*” numeric will be return “NaN”.

False, 0, the empty string (“”), undefined, null are all falsy values.

The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string (“”) count as false, while all the other values count as true.

For cases like this, where you do not want any automatic type conversions to happen, there are two extra operators: === and !==.

Recommend using the three-character comparison operators defensively to prevent unexpected type conversions.

When something that doesn’t map to a number in an obvious way (such as “five” or undefined) is converted to a number, the value NaN is produced.

Further arithmetic operations on NaN keep producing NaN, so if you find yourself getting one of those in an unexpected place, look for accidental type conversions.

When comparing values of the same type using ==, the outcome is easy to predict: you should get true when both values are the same, except in the case of NaN.

But when the types differ, JavaScript uses a complicated and confusing set of rules to determine what to do. In most cases, it just tries to convert one of the values to the other value’s type.

However, when null or undefined occurs on either side of the operator, it produces true only if both sides are one of null or undefined.

console.log(null == undefined);
// → true
console.log(null == 0);
// → false

When you want to test whether a value has a real value instead of null or undefined, you can simply compare it to null with the == (or !=) operator.

Short-circuiting of logical operators

The logical operators && and || handle values of different types in a peculiar way.

They will convert the value on their left side to Boolean type in order to decide what to do, but depending on the operator and the result of that conversion, they return either the original left-hand value or the right-hand value.

The || operator, for example, will return the value to its left when that can be converted to true and will return the value on its right otherwise. This conversion works as you’d expect for Boolean values and should do something analogous for values of other types.

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

This functionality allows the || operator to be used as a way to fall back on a default value. If you give it an expression that might produce an empty value on the left, the value on the right will be used as a replacement in that case.

The && operator works similarly, but the other way around. When the value to its left is something that converts to false, it returns that value, and otherwise it returns the value on its right.

Another important property of these two operators is that the expression to their right is evaluated only when necessary. In the case of true || X, no matter what X is—even if it’s an expression that does something terrible—the result will be true, and X is never evaluated. The same goes for false && X, which is false and will ignore X. This is called short-circuit evaluation.

The conditional operator works in a similar way. The first expression is always evaluated, but the second or third value, the one that is not picked, is not.