Which is best practice, please suggest

Supriya25
Tera Guru

Hi All,

Please suggest me which is best practice


var newcats=[];
var cat=['cat1','cat2','cat3','cat4','cat5','cat6'];

gs.info(typeof cat);   //Output: object
if(cat.toString().indexOf(gr.u_cat.toString())==-1){
newcats.push(gr.u_cat.toString());

}

 

OR

 

var newcats=[];
var cat=['cat1','cat2','cat3','cat4','cat5','cat6'];
gs.info(typeof cat);   //Output: object
if(cat.indexOf(gr.u_cat.toString())==-1){
newcats.push(gr.u_cat.toString());

}

 

1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @Supriya25 

Method 2 is more preferable. We don't need to convert array data into string as it already in string format.

If data is not in string format then go for method 1. 

 

If this helped please hit like and mark it as an accepted solution 

 

Thank You 

Juhi Poddar 

View solution in original post

4 REPLIES 4

Juhi Poddar
Kilo Patron

Hello @Supriya25 

Method 2 is more preferable. We don't need to convert array data into string as it already in string format.

If data is not in string format then go for method 1. 

 

If this helped please hit like and mark it as an accepted solution 

 

Thank You 

Juhi Poddar 

Thank Juhi.!

Runjay Patel
Giga Sage

Hi @Supriya25 ,

 

2nd approach is good, getting the value using indexOf from arrays is good way.

if you are using .toString() then it will convert the array in comma separated value and then perform the index of which is not required.

 

Accept the solution if it helped

Thanks Runjay.! I do follow your YouTube channel  .