Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Variable not declared before use

ID: js/use-before-declarationKind: problemSecurity severity: Severity: warningPrecision: very-highTags:   - quality   - maintainability   - readabilityQuery suites:   - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

JavaScript allows local variables to be used before they are declared. However, this is confusing for readers of the code, since it may not be clear whether the use refers to the local variable or to a global variable of the same name.

Recommendation

Move local variable declarations to the beginning of the enclosing function, or (for global variables) to the beginning of the source file.

Example

In the following example, the local variablex declared in functionf shadows the global variable of the same name. However, the first use of the local variable precedes its declaration, so a developer unfamiliar with the code might assume that this use refers to the global variable instead of the local variable.

varx=23;functionf(){x=42;varx;}

References

  • D. Crockford,#"https://twitter.com/github" title="GitHub on Twitter">


[8]ページ先頭

©2009-2025 Movatter.jp