Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | date.@@toPrimitive() function

  • Last Updated : 27 Sep, 2018

The date.@@toPrimitive() function is an inbuilt function in JavaScript which is used to convert the date object into a primitive value.

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :