-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathConstructors of Built-In Types.js
More file actions
45 lines (22 loc) · 1.11 KB
/
Copy pathConstructors of Built-In Types.js
File metadata and controls
45 lines (22 loc) · 1.11 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
// The constructors of the built in types like Number and String behave differently when being used with the new keyword and without it.
// new Number(10) === 10; // False, Object and Number
// Number(10) === 10; // True, Number and Number
// new Number(10) + 0 === 10; // True, due to implicit conversion
// Using a built-in type like Number as a constructor will create a new Number object, but leaving out the new keyword will make the Number function behave
// like a converter.
// In addition, passing literals or non-object values will result in even more type coercion.
// The best option is to cast to one of the three possible types explicitly.
// Casting to a String
console.log('' + 10 === '10'); // true
// By prepending an empty string, a value can easily be cast to a string.
// Casting to a Number
console.log(+'10' === 10); // true
// Casting to a Boolean
// By using the not operator twice, a value can be converted to a boolean.
// !!'foo'; // true
// !!''; // false
// !!'0'; // true
// !!'1'; // true
// !!'-1' // true
// !!{}; // true
// !!true; // true