Difference between arr+=(','+i) vs arr.push(i)

Supriya25
Tera Guru

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);

2 ACCEPTED SOLUTIONS

Community Alums
Not applicable

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.

View solution in original post

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

View solution in original post

5 REPLIES 5

Chaitanya ILCR
Kilo Patron

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 

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  ?

var arr=''; is declaring a string, var arr = []; is declaring an array.

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