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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

     
TypeError: Symbol.toPrimitive: expected "string", "number", or "default", got number


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.