Eloquent Javascript Chapter 1: Values, Types, Operators Flashcards
(23 cards)
Javascript vs. other languages
More liberal in what it allows. Good for flexibility, but makes it difficult to troubleshoot.
Bits
All data is stored as bits, any kind of two-valued thing (usually 0’s and 1’s). The weight of each 0 or 1 increases from right to left.
6 Types of Values
numbers, strings, Booleans, objects, functions, undefined values
Javascript and accuracy re: fractional numbers
Calculations not always accurate because numbers are stored using 64 bits.
Operators
Any symbol that causes an operation to occur: +, *, -, /, %
% Operator
The remainder or modulo operator. X % Y provides the remainder of dividing X by Y.
3 Special Numbers
Considered numbers but don’t behave like normal numbers: infinity, -infinity, NaN
NaN
Not a number - results from any operation that doesn’t yield a precise result, such as 0/0, infinity-infinity, etc.
Strings
Text in quotes. Can be marked with single and double quotes as long as beginning and end quotes match.
Escaping the character
Need to “escape” the character with a backslash before certain characters, such as quotes within quotes and enters/newlines. \n = newline, \t = tab. Actual backslash is \.
Concatenating strings
“text” + “text” = “texttext”
Binary operators
Operators that take two values: 1-2, 2+3, 3>1
Unary operators
Operators that take just one value. Includes operators that are not symbols, but words. Includes minus operator that can be used to make something negative. typeof() for example is a unary operator.
console.log
Show result on screen. Ultimate appearance depends on Javascript environment chosen to run.
Boolean values
Has only two possible values, true and false. console.log(3>2) results in true.
Comparisons
Using >, <=, >=, ==, !=. Note that the latter two are imprecise and sometimes value types can be changed. Alternately, use === and !== for exact values.
Can strings be compared with >,
Yes - they are compared based on Unicode standard, which assigns numbers to each character. Uppercase letters are valued less than lowercase ones.
3 Logical operators
and (&&), or (||), not(!). These can be applied to Boolean values/used to reason about Booleans.
&& vs. || precedence
&& has greater precedence than ||.
Ternary operator
Choice between two values is decided by a third. true ? 1 : 2 will return left value, false ? 1 : 2 will return right value.
2 Undefined values
null, undefined. Denote the absence of meaningful value, but are still values themselves. Are more or less interchangeable. Note that javascript might interpret null as 0. console.log(null==undefined) is true. console.log(null == 0) is false.
Type coercion
When an operator is applied to the “wrong” type of value, JavaScript will convert that value to the type it wants, with often unexpected/unwanted results. If NaN appears where unexpected, may be result of accidental type conversions.
Short-circuit evaluation
|| and && can return either left or right value. For ||, will return left value if it evaluates as true, else right value. && works opposite way, taking left if evaluates as false. This can be used to produce default values in the case that the left value evaluates as an NaN.