Java 005: Declaration and Assignment
2021/12/29 17:07:36
本文主要是介绍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 005: Declaration and Assignment的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南