Skip to main content

Assignment to global variable

In This Article

Note

The Assignment to global variable code issue is JavaScript specific.

CodeRush Classic shows the Assignment to global variable code issue if a value is assigned to a variable implicitly declared within a function.

#Fix

Declare the variable explicitly.

#Purpose

Highlights the assignment to a variable implicitly declared within a function, because it often denotes an incomplete code.

#Example

function ProcessData(data) {
    date = new Date();
    SaveData(date.getDate(), data);
    textElement = document.getElementById("Info");
    textElement.nodeValue = "DataSaved";
}

Fix:

function ProcessData(data) {
    var date = new Date();
    SaveData(date.getDate(), data);
    var textElement = document.getElementById("Info");
    textElement.nodeValue = "DataSaved";
}