(ES6) ES6 (2016)

Coersion (CR12)

   1:  const obj = {
   2:      valueOf() {
   3:          return 10;
   4:      },
   5:      toString() {
   6:          return "20";
   7:      }
   8:  };
   9:  
  10:  console.log(Number(obj)); // Output: 10 (valueOf() is called by Number())
  11:  console.log(5 + obj);    // Output: 15 (valueOf() is called, as hint is "number")
  12:  console.log("Value: " + obj); // Output: "Value: 20" (toString() called due to string concatenation)
  13:  
  14:  



ToPrimitive Coercion:
Object to Number (using valueOf and toString):

10
15
Value: 10





Coersion context:






ES6 context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/JavascriptES6/CR12.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>