Klassische lokale Variablen in JavaScript
Zitate
- I would advice only to use let and const. Consider var ‘legacy’ in ES6. Try to always use const if you don’t need to reassign the variable.
https://wecodetheweb.com/2015/05/27/var-let-or-const-what-to-use/
- Many have completely stopped using var, opting to use const for variables that won’t change and let otherwise. These new keywords provide more specific scope, less errors and improved readability/maintainability.
https://hackernoon.com/js-var-let-or-const-67e51dbb716f
- When to use var
I would say: almost never.
And I really mean that. I see very few good reasons why you might want to use var.
It would have to be because you really want function scoped variables. For the most part, that’s absolutely not necessary, and is only confusing.
let and const gives you all the functionality you actually need from var, but with none of the weird edge cases you really wouldn’t expect (unless you are already a complete JavaScript expert).
http://www.gjermundbjaanes.com/javascript-var-let-and-const-and-when-to-use-them/
- Use const for all of your references; avoid using var.
If you must reassign references, use let instead of var.
https://github.com/airbnb/javascript/issues/485
- I favor `const` over `let` in ES6.
I don’t use `var` in ES6. There is value in block scope for loops, but I can’t think of a situation where I’d prefer `var` over `let`.
medium.com/javascript-scene/javascript-es6-var-let-or-const-ba58b8dcde75
- ES6 introduced two new ways to declare variables, let and const.
...
Both provide better block scoping that var. const differs from let because the immediate value cannot be changed once it is declared.
Variables declared using var are function scoped, which has led to confusion to many developers as they start using in JavaScript
https://www.linkedin.com/pulse/declaring-winner-between-javascripts-var-let-const-chris-love
Notiz
Voraussichtlich werden »const« und »let« mit Blockgültigkeitsbereich (block scope ) in zukünftigen JavaScript -Versionen immer besser unterstützt werden.
Im Jahre 2014 sollte für Programme, die auf allen Browsern (und auch auf älteren) laufen sollen aber statt dessen »var« verwendet werden.
Nachdem »const« und »let« jetzt schon vorgestellt wurden, ist es aber relativ einfach, nun auch noch »var« zu erlernen.
function a() {
v = "Not global";
{
var v = 1;
}
console.log(v);
}
Übungsfragen *
? Einsetzen in eine Lücke *
Die Schreibweise »a…b:c« soll (nur in dieser Übungsfrage) bedeuten, daß der Text hinter dem Doppelpunkt »:« in die mit »…« gekennzeichneten Lücken in dem Text vor dem Ausrufezeichen eingesetzt werden soll.
- Welcher Text ergibt sich dann für »a…b:c«?
- Welcher Text ergibt sich dann für »a…b…d:c«?