== Why is this an issue?

Variable declaration is the process of creating a new variable and specifying its name. JavaScript provides three ways to declare variables: using the `var`, `let`, and `const` keywords.

* The `var` keyword is used to declare function-scoped or global-scoped variables, i.e. they are accessible throughout the function or the entire program, respectively.
* The `let` keyword is used to declare block-scoped variables, that is, variables accessible only within the nearest curly braces block where it is defined.
* The `const` keyword is used to declare variables that are constant, meaning their values cannot be reassigned.

Explicitly declaring variables improves code readability and maintainability. It makes it clear to other developers that you are creating a new variable and sets expectations about its scope. It also helps catch typos and avoid potential issues caused by accidentally reusing variable names.

If you assign a value to a variable without declaring it with `var`, `let`, or `const`, JavaScript treats it as an implicit global variable. Implicit globals can lead to unintended consequences and make it difficult to track and manage variables. They can cause naming conflicts, make code harder to understand, and introduce bugs that are hard to trace.

[source,javascript,diff-id=1,diff-type=noncompliant]
----
function f() {
  i = 1; // Noncompliant: i is global

  for (j = 0; j < array.length; j++) { // Noncompliant: j is global too
    // ...
  }
}
----

You should explicitly declare all the variables of your code. Use the `const` keyword if the variable is only assigned once and the `let` keyword otherwise.

[source,javascript,diff-id=1,diff-type=compliant]
----
function f() {
  const i = 1;

  for (let j = 0; j < array.length; j++) { 
    // ...
  }
}
----

== Resources
=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Glossary/Variable[Variable]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#declaring_variables[Declaring variables]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#variable_scope[Variable scope]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var[var]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let[let]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const[const]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Add the "let", "const" or "var" keyword to this declaration of "{0}" to make it explicit.


'''
== Comments And Links
(visible only on this page)

=== is related to: S3798

=== on 13 Mar 2015, 13:49:55 Ann Campbell wrote:
I modified the code sample you provided, [~linda.martin]. Feel free to change it back.

=== on 13 Mar 2015, 14:02:49 Linda Martin wrote:
\[~ann.campbell.2] From the description it seems that each type you declare a variable without the keyword "var" it creates a global variable. Whereas it is only within for-loops and functions that that it creates a global variable.

Maybe it's my understanding of english that it's questionable or I wrongly expressed myself when I first described the rule ?

=== on 19 May 2015, 14:09:17 Linda Martin wrote:
Reviewed.

endif::env-github,rspecator-view[]
