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

If a function in a JavaScript module changes a global variable, can other functions in that module see that change?

Consider the following example

data = null

function read_data() {
    //read a file and assign data = file content
}

function print() {
    console.log(data)
}

If I call read_data and then print from another script, would it print null? Or should I call read_data every time I need to print 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 :

Yes if read_data changes the global variable data, then other functions in the same module can see that change. If you call read_data first and then call print, print will display the updated value of data.

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