-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeCasting-Coercion.js
More file actions
65 lines (42 loc) · 1.77 KB
/
TypeCasting-Coercion.js
File metadata and controls
65 lines (42 loc) · 1.77 KB
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
//: 19.1 => Perform type coercion at the beginning of the statement.
//: 19.2 => String: eslint: no-new-wrappers
// bad
const totalScore = new String(this.reviewScore); // typeof totoalscore is "object" not "string"
// bad
const totalScore2 = this.reviewScore + ""; // invokes this.reviewScore.valueOf()
// bad
const totalScore3 = this.reviewScore.toString(); // isn't guranteed to return a string
// good
const totalScore4 = String(this.reviewScore);
//: 19.3 => /* Numbers Use "Number" for type casting and parseInt always with a radix for parsing strings. eslint: redix, no-new-wrappers */
const inputValue = 4;
// bad
const val = new Number(inputValue);
// bad
const val2 = +inputValue;
// bad
const val3 = inputValue >> 0;
// bad
const val4 = parseInt(inputValue);
// good
const val5 = Number(inputValue);
// good
const val6 = parseInt(inputValue, 10);
//: 19.4 => /* If for whatever reason you are doing something wild and parseInt is your bottleneck and need to use Bitshift for performance reasons, leave a comment explainning why and what you're doing. */
// good
/* parseInt was the reason my code was slow.
Bitshifting the string to coerce it to a
Number made it a lot faster. */
const val7 = inputValue >> 0;
//: 19.5 => /* Note: BE CAREFUL when using "bitshift" operations. Numbers are represented as 64-bit values, but bitshift operations always return a 32-bit interger. Bishift can lead to unexpected behavior for integer value larger than 32 bits. Largerst signed 32-bit int is 2,147,483,647 */
2147483647 >> 0; // => 2147483647
2147483648 >> 0; // => -2147483648
2147483649 >> 0; // => -2147483647
//: 19.6 => Booleans: eslint: no-new-wrappers
const age = 0;
// bad
const hasAge = new Boolean(age);
// good
const hasAge2 = Boolean(age);
// best
const hasAge3 = !!age;