valueOf() method

The valueOf() method in Apps Script returns the primitive value of a String object. This method is useful when we need to extract the actual value of a string object for further processing or manipulation.

For example, let’s say we have a variable myString that contains a string value ‘123’. We can use the valueOf() method to extract the actual numeric value as follows:

var myString = '123';
var myNumber = Number(myString.valueOf()); //returns 123

Another example could be if we have a string that contains a date value like ‘2022-06-01’ and we want to extract the year, month and date as separate variables. We can use the valueOf() method to extract the string value and then manipulate it using other methods like split() or substring().

var myDate = '2022-06-01';
var year = Number(myDate.valueOf().substring(0,4)); //returns 2022
var month = Number(myDate.valueOf().substring(5,7)); //returns 06
var day = Number(myDate.valueOf().substring(8,10)); //returns 01

In the above example, we first extract the string value using valueOf() and then use substring() method to extract year, month and date as separate variables.

Overall, the valueOf() method is useful when we need to extract the primitive value of a string object for further processing or manipulation.