- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2025 11:08 AM
Hi All,
Can someone help me to understand what is difference between below two
When should we use : Arr.push(i) ;
When should we use : Arr + = ( ' , ' + i);
var criticalIncident = [];
var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.addQuery('priority', '1');
inc.addQuery('category', 'software');
inc.query();
while (inc.next()) {
criticalIncident.push(inc.number); OR criticalIncident+=(','+inc.number);
}
gs.info("Critical Incidents: " + criticalIncident);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2025 06:18 PM
When to use criticalIncident.push(inc.number);
Here, criticalIncident is an array.
.push() adds each incident number as a separate item in the array.
At the end, you can use .join(',') to turn that array into a single comma-separated string if you want to print it nicely.
This is the cleanest and recommended way, because you keep your data structured.
When to use criticalIncident += ',' + inc.number;
Here, criticalIncident is just a string, not an array.
You keep adding each incident number directly to that string, separated by commas.
This works too, but it can lead to things like an extra comma at the start, and you lose the benefit of having an actual array you can work with.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2025 06:23 PM
Hi @Supriya25 ,
var arr = '' (here arr is not an array it's a string)
var arr = [] (here arr is an array)
Better use the .push() method when dealing with arrays and use join() method to convert the array to string
string concatenation works but why to create a variable of type array and convert it to string at the same movement (you can directly initialize it as a string by setting value to var arr ='')
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2025 11:16 AM
Hi @Supriya25 ,
Are.push() appends the element to the array and keep it as array (object)
Where the Arr+=. Convert the array into string and add the value to the string
In this scenario that you've shared bothe the approches will work
Regards
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2025 02:04 PM
Thanks Chaitanya !
one additional question here please.
Some time we declare Array variables in two ways
var arr =' '; // this one also holds multiple values
var arr = []; // this one also holds multiple values
could help with major difference ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2025 02:06 PM
var arr=''; is declaring a string, var arr = []; is declaring an array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2025 06:23 PM
Hi @Supriya25 ,
var arr = '' (here arr is not an array it's a string)
var arr = [] (here arr is an array)
Better use the .push() method when dealing with arrays and use join() method to convert the array to string
string concatenation works but why to create a variable of type array and convert it to string at the same movement (you can directly initialize it as a string by setting value to var arr ='')
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya