{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1055,"path":"api-reference/server-api-reference/c_JSUtilAPI.md","title":null,"bundle":null,"doc_type":null,"release":null,"body":"---\ntitle: JSUtil - Global\ndescription: The JSUtil script include provides shortcuts for common JavaScript routines.Checks if item is null or is undefined.Escape ampersands commonly used to define URL attributes.Escapes invalid XML characters such as \"&lt; &gt; &amp;\".Returns the value in a boolean GlideRecord field.Checks if item is not null and is not undefined.Checks to see if the specified object is a member of the specified class.Checks if the specified object is a Java class.Logs all the properties in the given object: name, type, and value.Checks if item is null, undefined, or evaluates to the empty string.Checks if an item is null, undefined, or evaluates to the empty string.Converts the specified object to a Boolean.Determines the type of the specified object.Restore ampersands from escaped text.Removes escape characters.\nlocale: en-US\ncanonical_url: https://www.servicenow.com/docs/r/australia/api-reference/server-api-reference/c\\_JSUtilAPI.html\nrelease: australia\nproduct: Server API Reference\nclassification: server-api-reference\ntopic_type: concept\nlast_updated: \"2026-03-12\"\nreading_time_minutes: 7\nbreadcrumb: [Server API reference, API reference, API implementation and reference]\n---\n\n# JSUtil- Global\n\nThe JSUtil script include provides shortcuts for common JavaScript routines.\n\nThis API is available in global server-side scripts, not scoped scripts.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## JSUtil - doesNotHave\\(Object item\\)\n\nChecks if item is null or is undefined.\n\n|Name|Type|Description|\n|----|----|-----------|\n|item|Object|The object to check|\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the specified object is null or undefined.|\n\n```\nvar x = \"the quick brown fox\";\nvar y = \"\";\nvar z;\n \ngs.print(\"x = '\" + x + \"', JSUtil.doesNotHave(x) = \" + JSUtil.doesNotHave(x));\ngs.print(\"y = '\" + y + \"', JSUtil.doesNotHave(y) = \" + JSUtil.doesNotHave(y));\ngs.print(\"z = '\" + z + \"', JSUtil.doesNotHave(z) = \" + JSUtil.doesNotHave(z))\n```\n\nOutput:\n\n```\nx = 'the quick brown fox', JSUtil.doesNotHave(x) = false\ny = '', JSUtil.doesNotHave(y) = false\nz = 'undefined', JSUtil.doesNotHave(z) = true\n```\n\n## JSUtil - escapeAttr\\(String text\\)\n\nEscape ampersands commonly used to define URL attributes.\n\n|Name|Type|Description|\n|----|----|-----------|\n|text|String|The text|\n\n|Type|Description|\n|----|-----------|\n|String|The text with ampersands properly escaped.|\n\n```\nvar attr = \"sysparm_query=active=true&sysparm_view=special\";\n \ngs.print(JSUtil.escapeAttr(attr));\n```\n\nOutput: This is the returned text. If the text is displayed in the application, the page will render the escaped ampersand with a single ampersand.\n\n```\nsysparm_query=active=true&sysparm_view=special\n```\n\n## JSUtil - escapeText\\(String text\\)\n\nEscapes invalid XML characters such as \"&lt; &gt; &amp;\".\n\n|Name|Type|Description|\n|----|----|-----------|\n|text|String|The text|\n\n|Type|Description|\n|----|-----------|\n|String|The text with escape characters added.|\n\n```\nvar html = \"<b>This is my title</b>\";\n \ngs.print(JSUtil.escapeText(html));\n```\n\nOutput: This is the value returned. If the result is displayed in the application, the page renders the brackets back so it appears that it is not escaped.\n\n```\n<b>This is my title</b>\n```\n\n## JSUtil - getBooleanValue\\(GlideRecord now\\_GR, String field\\)\n\nReturns the value in a boolean GlideRecord field.\n\n|Name|Type|Description|\n|----|----|-----------|\n|now\\_GR|GlideRecord|A GlideRecord|\n|field|String|The field from which to retrieve the boolean value.|\n\n|Type|Description|\n|----|-----------|\n|Boolean|Returns the value in a boolean GlideRecord field, returns true if value of field is true, \"true\", 1, or \"1\".|\n\n```\nvar inc = new GlideRecord(\"incident\");\n//get an active incident\ninc.addActiveQuery();\ninc.setLimit(1);\ninc.query();\ninc.next();\n \ngs.print(JSUtil.getBooleanValue(inc, \"active\"));\n```\n\nOutput: true\n\n## JSUtil - has\\(Object item\\)\n\nChecks if item is not null and is not undefined.\n\n|Name|Type|Description|\n|----|----|-----------|\n|item|Object|The Object to check|\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the specified object is not null and is not undefined.|\n\n```\nvar x = \"the quick brown fox\";\nvar y = \"\";\nvar z;\n \ngs.print(\"x = '\" + x + \"', JSUtil.has(x) = \" + JSUtil.has(x));\ngs.print(\"y = '\" + y + \"', JSUtil.has(y) = \" + JSUtil.has(y));\ngs.print(\"z = '\" + z + \"', JSUtil.has(z) = \" + JSUtil.has(z));\n```\n\nOutput:\n\n```\nx = 'the quick brown fox', JSUtil.has(x) = true\ny = '', JSUtil.has(y) = true\nz = 'undefined', JSUtil.has(z) = false\n```\n\n## JSUtil - instance\\_of\\(Object item, String class\\)\n\nChecks to see if the specified object is a member of the specified class.\n\nFor JavaScript objects, this method behaves exactly like the JavaScript operator \"instanceof\", but also supports Java objects.\n\n|Name|Type|Description|\n|----|----|-----------|\n|item|Object|The object to check|\n|class|String|The class to check|\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the specified object is a member of the specified class.|\n\n```\nvar a = ['a','b','c'];\nvar b = 10;\nvar c = new GlideRecord(\"incident\");\n \ngs.print(\"JSUtil.instance_of(a,'Array') = \" + JSUtil.instance_of(a,Array));\ngs.print(\"JSUtil.instance_of(a,'String') = \" + JSUtil.instance_of(a,String));\n \ngs.print(\"JSUtil.instance_of(b,'String') = \" + JSUtil.instance_of(b,String));\n \ngs.print(\"JSUtil.instance_of(c,'GlideRecord') = \" + JSUtil.instance_of(c,GlideRecord));\n```\n\nOutput:\n\n```\nJSUtil.instance_of(a,'Array') = true\nJSUtil.instance_of(a,'String') = false\nJSUtil.instance_of(b,'String') = false\nJSUtil.instance_of(c,'GlideRecord') = true\n```\n\n## JSUtil - isJavaObject\\(Object value\\)\n\nChecks if the specified object is a Java class.\n\n|Name|Type|Description|\n|----|----|-----------|\n|value|Object|The object to check|\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the specified object is an instance of a Java class.|\n\n```\nvar tu = new TableUtils(\"incident\");\nvar classes = tu.getHierarchy(); //Java ArrayList\nvar tables = [\"task, incident\"]; //JavaScript Array\n \ngs.print(\"JSUtil.isJavaObject(classes) = \" + JSUtil.isJavaObject(classes));\ngs.print(\"JSUtil.isJavaObject(tables) = \" + JSUtil.isJavaObject(tables));\n```\n\nOutput:\n\n```\nJSUtil.isJavaObject(classes) = true\nJSUtil.isJavaObject(tables) = false\n```\n\n## JSUtil - logObject\\(Object obj, String name\\)\n\nLogs all the properties in the given object: name, type, and value.\n\nOutput is written to the console if you are running from a background script or have debug logging enables. The output is also written to the system log.\n\n|Name|Type|Description|\n|----|----|-----------|\n|obj|Object|The object for which to enumerate properties|\n|name|String|Optional name for the logged object|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar arr = [\"a\",\"b\",\"c\"];\n \nvar inc = new GlideRecord(\"incident\");\n//get an active incident\ninc.addActiveQuery();\ninc.setLimit(1);\ninc.query();\ninc.next();\n \nJSUtil.logObject(arr, \"arr\");\nJSUtil.logObject(inc, \"inc\");\n```\n\nOutput:\n\n```\nLog Object: arr\n  Array of 3 elements\n    [0]: string = a\n    [1]: string = b\n    [2]: string = c\nLog Object: inc\n  GlideRecord('incident') @ INC0000002\n```\n\n## JSUtil - nil\\(Object item\\)\n\nChecks if item is null, undefined, or evaluates to the empty string.\n\n|Name|Type|Description|\n|----|----|-----------|\n|item|Object|The object to check|\n\n|Type|Description|\n|----|-----------|\n|Boolean|True if the item is null, undefined, or evaluates to the empty string.|\n\n```\nvar x = \"the quick brown fox\";\nvar y = \"\";\nvar z;\n \ngs.print(\"x = '\" + x + \"', JSUtil.nil(x) = \" + JSUtil.nil(x));\ngs.print(\"y = '\" + y + \"', JSUtil.nil(y) = \" + JSUtil.nil(y));\ngs.print(\"z = '\" + z + \"', JSUtil.nil(z) = \" + JSUtil.nil(z));\n```\n\nOutput:\n\n```\nx = 'the quick brown fox', JSUtil.nil(x) = false\ny = '', JSUtil.nil(y) = true\nz = 'undefined', JSUtil.nil(z) = true\n```\n\n## JSUtil - notNil\\(Object item\\)\n\nChecks if an item is null, undefined, or evaluates to the empty string.\n\n**Note:** This method returns an error if the object provided isn’t declared.\n\n|Name|Type|Description|\n|----|----|-----------|\n|item|Object|The object to check.|\n\n<table id=\"table_urk_f35_4t\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nBoolean\n\n</td><td>\n\nFlag that indicates whether the item exists and isn’t empty. An error returns if the object provided isn’t declared.Valid values:\n\n-   true: The object provided has been declared and defined. If the object is a string, it isn’t empty.\n-   false: The object provided is null, undefined, or evaluates to an empty string.\n\n</td></tr></tbody>\n</table>The following example shows results for a string variable, an empty string variable, and an undefined variable.\n\n```\nvar x = \"the quick brown fox\";\nvar y = \"\";\nvar z;\n\ngs.print(\"x = '\" + x + \"', JSUtil.notNil(x) = \" + JSUtil.notNil(x));\ngs.print(\"y = '\" + y + \"', JSUtil.notNil(y) = \" + JSUtil.notNil(y));\ngs.print(\"z = '\" + z + \"', JSUtil.notNil(z) = \" + JSUtil.notNil(z));\n```\n\nOutput:\n\n```\nx = 'the quick brown fox', JSUtil.notNil(x) = true\ny = '', JSUtil.notNil(y) = false\nz = 'undefined', JSUtil.notNil(z) = false\n```\n\n## JSUtil - toBoolean\\(Object item\\)\n\nConverts the specified object to a Boolean.\n\n|Name|Type|Description|\n|----|----|-----------|\n|item|Object|The object to convert|\n\n|Type|Description|\n|----|-----------|\n|Boolean|If the specified object is a boolean, it is passed through. Non-zero numbers return true. Null or undefined return false. Strings return true only if exactly equal to 'true'.|\n\n```\nvar zero = 0;\nvar one = 1;\nvar number = 12;\nvar trueBoolean = true;\nvar trueString = \"true\";\nvar otherString = \"random text\";\n \ngs.print(\"JSUtil.toBoolean(zero) = \" + JSUtil.toBoolean(zero));\ngs.print(\"JSUtil.toBoolean(one) = \" + JSUtil.toBoolean(one));\ngs.print(\"JSUtil.toBoolean(number) = \" + JSUtil.toBoolean(number));\ngs.print(\"JSUtil.toBoolean(trueBoolean) = \" + JSUtil.toBoolean(trueBoolean));\ngs.print(\"JSUtil.toBoolean(trueString) = \" + JSUtil.toBoolean(trueString));\ngs.print(\"JSUtil.toBoolean(otherString) = \" + JSUtil.toBoolean(otherString));\n```\n\nOutput:\n\n```\nJSUtil.toBoolean(zero) = false\nJSUtil.toBoolean(one) = true\nJSUtil.toBoolean(number) = true\nJSUtil.toBoolean(trueBoolean) = true\nJSUtil.toBoolean(trueString) = true\nJSUtil.toBoolean(otherString) = false\n```\n\n## JSUtil - type\\_of\\(Object value\\)\n\nDetermines the type of the specified object.\n\n|Name|Type|Description|\n|----|----|-----------|\n|value|Object|The object to check|\n\n<table id=\"table_jc5_lj5_4t\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nString\n\n</td><td>\n\nThe type of the specified object. -   'null' if the given value is null or undefined\n-   'string' if the given value is a primitive string or a String wrapper instance\n-   'number' if the given value is a primitive number or a Number wrapper instance\n-   'boolean' if the given value is a primitive boolean or a Boolean wrapper instance\n-   'function' if the given value is a function\n-   'object' otherwise\n\n</td></tr></tbody>\n</table>```\nvar a = [\"a\",\"b\",\"c\"];\nvar b = 10;\nvar c = new GlideRecord(\"incident\");\nvar d = true;\nvar e;\n \ngs.print(\"JSUtil.type_of(a) = \" + JSUtil.type_of(a));\ngs.print(\"JSUtil.type_of(b) = \" + JSUtil.type_of(b));\ngs.print(\"JSUtil.type_of(c) = \" + JSUtil.type_of(c));\ngs.print(\"JSUtil.type_of= \" + JSUtil.type_of(d));\ngs.print(\"JSUtil.type_of(e) = \" + JSUtil.type_of(e));\n```\n\nOutput:\n\n```\nJSUtil.type_of(a) = object\nJSUtil.type_of(b) = number\nJSUtil.type_of(c) = object\nJSUtil.type_of= boolean\nJSUtil.type_of(e) = null\n```\n\n## JSUtil - unescapeAttr\\(String text\\)\n\nRestore ampersands from escaped text.\n\n|Name|Type|Description|\n|----|----|-----------|\n|text|String|The text|\n\n|Type|Description|\n|----|-----------|\n|String|The text with escape characters removed.|\n\n```\nvar attr = \"sysparm_query=active=true&amp;sysparm_view=special\";\n \ngs.print(JSUtil.unescapeAttr(attr));\n```\n\nOutput:\n\n```\nsysparm_query=active=true&sysparm_view=special\n```\n\n## JSUtil - unescapeText\\(String text\\)\n\nRemoves escape characters.\n\n|Name|Type|Description|\n|----|----|-----------|\n|text|String|The text to process.|\n\n|Type|Description|\n|----|-----------|\n|String|The the text without escape characters.|\n\n```\nvar html = \"&lt;b&gt;This is my title&lt;/b&gt;\";\n \ngs.print(JSUtil.unescapeText(html));\n```\n\nOutput: This is the value returned. If the text is displayed in the application the page, it renders the html tags and displays the text in bold.\n\n```\n<b>This is my title</b>\n```\n\n"}}