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

Advertisements

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?

>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.

Leave a ReplyCancel reply