-
Notifications
You must be signed in to change notification settings - Fork 0
/
plusPlus.js
51 lines (38 loc) · 1.13 KB
/
plusPlus.js
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
// example 1
console.log(1 + 1)
console.log(1 + + 1)
console.log(1 + + 1 + 1)
console.log(1 + + 1 + + 1)
console.log(1 + + + 1)
console.log(1 + + '1' + + '1')
console.log('1' + + '1' + + '1')
console.log('a' + + 'b')
console.log('a' + + 'b' + 'c')
console.log('a' + + 'b' + + 'c')
// In mathematical operators, + works on both numbers and strings (used in string concatenation).
// Hence, if any of the operands is not a number, using + converts all operand/s to string and concatenates.
console.log('1' + + '1' + + '1') // "1" + (+'1') + (+'1') = "1" + 1 + 1 = "1" + "1" + "1" = "111"
console.log('a' + + 'b') // "a" + (+'b') = a + "NaN" = "aNaN"
// example 2
let a = 1
console.log(a++ + a)
let b = 1
console.log(b + + + b)
let c = 1
console.log(c-- - c)
let d = 1
console.log(d - - - d)
/*
let a = 1
console.log(a +++ a) // 3
// (a++ + a) = 1 + 2 = 3 (Note that a after + gets incremented)
let b = 1
console.log(b + + + b) // 2
// (1 + +(+1)) = (1 + +1) = 1 + 1 = 2
let c = 1
console.log(c --- c) // 1
// (c-- - c) = 1 - 0 = 1 (Note that c after - gets decremented)
let d = 1
console.log(d - - - d) // 0
// (1 - -(-1)) = (1 - 1) = 0
*/