Some help understanding "block/scope" variables "dangers"

So, I have a function, and I’d like it to return multiple variables. To do that however, it seems like I need to run the function twice. E.g.

console.log( textCleanup(text,true,true,true,true,true,true)[0] )
console.log( textCleanup(text,true,true,true,true,true,true)[1] )

Now, I figured, I can just define the variables beyond the function, and have the function change them, then I don’t need to run it twice. But, is it safe? I haven’t been programming for very long and I feel like I night be missing information about how it all works, and to me it feels like maybe this isn’t how this should be done. Would appreciate any info on this. I tried googling it, but I suppose I wasn’t googling for the right terms, as I haven’t found much about it.

>Solution :

You’re confusing the terminology here. The function doesn’t return a variable. It returns a value. (In this case that value appears to be a reference to an array.) You can use a variable to store that value to reference it at a later time:

const myVar = textCleanup(text,true,true,true,true,true,true);
console.log( myVar[0] );
console.log( myVar[1] );

Leave a Reply