toString() method actual use case
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-27-2022 11:27 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-27-2022 11:58 AM
Hi, unfortunately your question\issue\context is not clear, I suspect that your tests show your data is already string type as 1/short_description is a string field. 2/you are looking at client side\web browser form data and browser\html data is string format. You do state how you are testing validating your data type? Normally you would use typeof() to validate the data type as printing/logging does not identify the type of data.
JavaScript typeof (w3schools.com)
toString() or String() method is used to convert non string values to string, the most commonly converted values would be integers, arrays, and elements of objects derived from ServiceNow API's like GlideRecord queries.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-27-2022 12:04 PM
If a field or variable is already a string, such as short_description, it's not necessary to use the toString() method to force it to a string. This is most useful when pushing a sys_id to an array, as not using toString() will cause it to store the memory location in the array, not the actual sys_id, so you'll get an array of all of the same sys_id. I also always put toString() on the end of object value assignments to prevent unexpected results. You'll also need this before doing any string manipulation methods. In summary, it can't hurt if it's redundant, and can save you a lot of hair pulling to get in the habit of forcing values to strings.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-27-2022 12:09 PM
getValue() return string equivalent of the value.
If you want to test this out, you can try using "typeof" operator.
var gr = g_form.getValue('short_description');
gs.info(typeof gr);
Aman Kumar