When a non-existent variable is referenced a ReferenceError is raised.
Due to the dynamic nature of JavaScript this can happen in a number of scenarios:
let or const before declaration (unlike var-declarations, they are not
hoisted to the top of the scope). let- and const-declarations (they have block scope, unlike
var-declarations, having function scope). this.). This rule does not raise issues on global variables which are defined with sonar.javascript.globals and
sonar.javascript.environments properties.
var john = {
firstName: "john",
show: function() { console.log(firstName); } // Noncompliant: firstName is not defined
}
john.show();
var john = {
firstName: "john",
show: function() { console.log(this.firstName); }
}
john.show();