-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJavaScript Allonge
71 lines (63 loc) · 2.58 KB
/
JavaScript Allonge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Prelude: Values and Expressions over Coffee
All values are expressions:
Type "42" into JavaScript and gets the same thing back - it is both "expression" and "value"
Ex.
42 // => 42
Other expressions
Ex.
"JavaScript" + " " + "Allonge" // => "JavaScript Allonge"
"JavaScript" - a string is a value
"Allonge" - another string is also a value
+ operator combined with strings makes an expressions
Values and identity
=== operator tests if two values are identical
!== operator tests if two values are NOT identical
Ex.
2 === 2 // => true
'hello' !== 'goodbye' // => true
How do "===" and "!==" work with the possibilities of different combinations of operands?
1. Two operands of different types - NEVER identical
Ex.
2 === '2' // => false
true !== 'true' // => true
2. Two operands of same type but holds different value - Not identical
Ex.
true === flase // => false
2 !== 5 // => true
'two' === 'five'// => false
3. Two operands of same "primitive" / "value" types (string, number and boolean)
and same content - identical
Ex.
2 + 2 === 4 // => true
(2 + 2 === 4) === (2 !== 5) // => true
4. Two operands of same "reference" types (array, functions) and same content - Not identical
Each time expression is evaluated, a new reference value is created
Ex.
[2-1, 2, 2+1] === [1, 2, 3] // => false
[1,2,3] === [1, 2, 3] // => false
[1, 2, 3] === [1, 2, 3] // => false
A Rich Aroma: Basic Numbers
number literals can have different bases - like base 8, base 10
numbers of different bases have same internal representation double-precision floating point
largest integer that can be used safely in JS is 2^53 -1
For some floating point number, there is no exact representation of fractions in base 10 as in base 2
Ex.
0.1 + 0.1 + 0.1 // => 0.30000000000000004
$43.21 is not the same as 43.21
operations on numbers
| and & operate directly on numbers' binary representation
Ex.
5 & 1 = 0101 & 0001 = 0001 = 1
5 | 1 = 0101 | 0001 = 0101 = 5
The first sip: Basic Functions
As little as possible about functions, but no less
Functions = computations to be performed
Ex. a function takes no values and return zero
() => 0
(() => 0) // => () => 0 in chrome
=> [Function] in Node.js
functions and identities
Function is a reference type
Ex. Reference type are not identical even with the same content
(() => 0) === (() => 0) //=> false
applying functions