forked from degranda/jsBasico-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6-coercion.js
46 lines (26 loc) · 727 Bytes
/
6-coercion.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
// La forma de convertir un type a otro: Coercion
// === Implicit Coercion ===
var a = 20;
var b = a + "";
// más ejemplos
var a = [1,2];
var b = [3,4];
a + b // En este ejemplo, por la concatenación, ambos arrays son obligados a convertirse en strings, y luego se juntan.
// === Explicit coercion ===
var c = String( a );
var d = Number( c );
// Otro ejemplo
var a = 30;
var b = a.toString();
var c = "100";
var d = +c; // Unary operator '+' de forma explícita convierte a número
// más ejemplos
var num1 = "3.14";
var num2 = 5 + +num1;
/*
Number + Number = Number
Number + String = String
String + Number = String
String + String = String
*/
// Todos las propiedades de valor, son strings