Skip to content

Files

Latest commit

 

History

History
48 lines (34 loc) · 859 Bytes

no-undef.md

File metadata and controls

48 lines (34 loc) · 859 Bytes

Pattern: Reference to undeclared variable

Issue: -

Description

Using variables that have not been declared leads to a ReferenceError at runtime. This often happens due to typos in variable names or forgetting to import or declare a variable. Always declare variables before use.

Examples

Example of incorrect code:

function foo() {
  bar = 3;  // bar is not declared
}

console.log(undeclared);

if (someUndefined) {
  handleCase();
}

obj.someFunction();  // obj is not declared

const sum = a + b;  // a and b are not declared

Example of correct code:

let bar;
function foo() {
  bar = 3;
}

const undeclared = 'now declared';
console.log(undeclared);

const someUndefined = condition;
if (someUndefined) {
  handleCase();
}

const obj = {};
obj.someFunction();

const a = 1, b = 2;
const sum = a + b;