Java 005: Declaration and Assignment
作者:互联网
let
let i, sum;
- Declare and assign an initial value to your variables:
let message = "hello";
let i = 0, j = 0, k = 0;
let x = 2, y = x*x; // Initializers can use previously declared variables
If you don’t specify an initial value, the value is "undefined"
- Using let in for loop
for(let i = 0, len = data.length; i < len; i++) console.log(data[i]);
for(let datum of data) console.log(datum);
for(let property in object) console.log(property);
const
const H0 = 74; // Hubble constant (km/s/Mpc)
const C = 299792.458; // Speed of light in a vacuum (km/s)
const AU = 1.496E8; // Astronomical Unit: distance to the sun (km)
- Constants cannot have their values changed, and any attempt to do so causes a TypeError to be thrown.
- const works just like let except that you must initialize the constant when you declare it
- Using const in for loop
for(const datum of data) console.log(datum);
for(const property in object) console.log(property);
In this case, the const declaration is just saying that the value is constant for the duration of one loop iteration
Variable Scope
- All variables and constants defined with let and const are block scoped
- Everything declared as part of the for loop are scoped by the loop body
- Variables declared at the top level are global scoped, i.e. the file that it is defined in
Repeated Declarations
- Ilegal to declare same variable twice in the same scope
- Legal to declare same variable twice in the nested scope
const x = 1; // Declare x as a global constant
if (x === 1) {
let x = 2; // Inside a block x can refer to a different value
console.log(x); // Prints 2
}
console.log(x); // Prints 1: we're back in the global scope now
let x = 3; // ERROR! Syntax error trying to re-declare x
Dynamically Typed
Javascript variables can hold a value of any type.
let i = 10;
i = "ten";
var
var x;
var data = [], count = data.length;
for(var i = 0; i < count; i++) console.log(data[i]);
- Function scoped: Scoped to the body of the containing function no matter how deeply nested they are inside that function.
- var outside of a function body, is a global variable.
- That variable is added to the property of globalThis object
- e.g. var x = 3; globalThis.x // => 3
- However, global let or const are not properties of globalThis
- Legal to repeatedly declare the same variable multiple times with var
" Unlike variables declared with
let
, it is legal to declare the same variable multiple times withvar
. And becausevar
variables have function scope instead of block scope, it is actually common to do this kind of redeclaration. The variablei
is frequently used for integer values, and especially as the index variable offor
loops. In a function with multiplefor
loops, it is typical for each one to beginfor(var i = 0; ...
. Becausevar
does not scope these variables to the loop body, each of these loops is (harmlessly) re-declaring and re-initializing the same variable. "
Hoisting
When a variable is declared with
var
, the declaration is lifted up (or “hoisted”) to the top of the enclosing function.
- So variables declared with var can be used, without error, anywhere in the enclosing function.
- Therefore, there is a difference in behavior of let and var
- If you declare a variable with var but attempt to use it before initialization & before the var statement, you get an undefined value.
- If you declare a variable with let but attempt to use it before the let statement runs, you will get an actual error instead of just seeing an undefined value.
标签:Java,Assignment,variables,console,variable,let,005,var,const 来源: https://blog.csdn.net/DOITJT/article/details/122215764