If a variable that is not supposed to change is not marked as const, it could be accidentally reassigned elsewhere in the code,
leading to unexpected behavior and bugs that can be hard to track down.
By declaring a variable as const, you ensure that its value remains constant throughout the code. It also signals to other developers
that this value is intended to remain constant. This can make the code easier to understand and maintain.
In some cases, using const can lead to performance improvements. The compiler might be able to make optimizations knowing that the
value of a const variable will not change.
Mark the given variable with the const modifier.
function seek(input) {
let target = 32; // Noncompliant
for (const i of input) {
if (i === target) {
return true;
}
}
return false;
}
function seek(input) {
const target = 32;
for (const i of input) {
if (i === target) {
return true;
}
}
return false;
}
function getUrl(protocol, domain, path) {
let url; // Noncompliant
url = `${protocol}/${domain}/${path}`;
return url;
}
function getUrl(protocol, domain, path) {
const url = `${protocol}/${domain}/${path}`;
return url;
}