JavaScript | date.@@toPrimitive() function
The date.@@toPrimitive() function is an inbuilt function in JavaScript which is used to convert the date object into a primitive value.
Syntax:
Dateobj[Symbol.toPrimitive](hint);
Parameter: This function accepts single parameter. Depending on the argument, the method can return either a string or a number.
Return Values: It returns the primitive value of the given date object.
Below programs illustrate the @@toPrimitive() function in JavaScript.
Program 1: When hint is default, [@@toPrimitive]() tries to call the toString method and if toString method does not exist it tries to call valueOf method.
Example:
<script> // Here a date has been assigned // while creating Date object var dateobj = new Date(); // converting the date object to a primitive value var result = dateobj[Symbol.toPrimitive]("default") // Printing year document.write(result); </script> |
Output:
Thu Sep 27 2018 12:49:02 GMT+0530 (India Standard Time)
Program 2: When hint is number, [@@toPrimitive]() tries to call the valueOf method and if valueOf method does not exist, it calls toString method.
Example:
<script> // Here a date has been assigned // while creating Date object var dateobj = new Date(); // converting the date object to a primitive value var result = dateobj[Symbol.toPrimitive]("number") // Printing year document.write(result); </script> |
Output:
1538032776898
Program 3: When hint is string, [@@toPrimitive]() tries to call the toString method and if toString method does not exist it tries to call valueOf method.
Example:
<script> // Here a date has been assigned // while creating Date object var dateobj = new Date(); // converting the date object to a primitive value var result = dateobj[Symbol.toPrimitive]("string") // Printing year document.write(result); </script> |
Output:
Thu Sep 27 2018 12:50:04 GMT+0530 (India Standard Time)
Note: Output may vary as per the current date and time.
Errors and Exceptions: When hint is anything other than “string”, “default” or”number”, [@@toPrimitive]() tries to call the toString method and if toString method does not exist it tries to call valueOf method and if valueOf method is aloso mot applicable then [@@toPrimitive]() throws a TypeError.
Example:
<script> // Here a date has been assigned // while creating Date object var dateobj = new Date(); // converting the date object to a primitive value var result = dateobj[Symbol.toPrimitive](90) // Printing year document.write(result); </script> |
Output:
TypeError: Symbol.toPrimitive: expected "string", "number", or "default", got number
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- JavaScript | toPrecision( ) Function
- JavaScript | toFixed( ) Function
- JavaScript | Function Parameters
- JavaScript | Array.of() function
- JavaScript | Math.E() function
- JavaScript | Function Definitions
- JavaScript | Function Call
- JavaScript | Array some() function
- Javascript | Number() Function
- How to override a JavaScript function ?
- JavaScript | Math.abs( ) function
- JavaScript | String() Function
- JavaScript | Function Invocation
- JavaScript | Function Apply
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



