Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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] );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading