Javascript 005: Declaration and Assignment

本文探讨了JavaScript中let和const的关键特性,包括初始值设定、在for循环中的使用、变量作用域(块级和全局),以及var的不同行为。重点讲解了变量声明的重复性和hoisting现象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  1. All variables and constants defined with let and const are block scoped
  2. Everything declared as part of the for loop are scoped by the loop body
  3. Variables declared at the top level are global scoped, i.e. the file that it is defined in

Repeated Declarations

  1. Ilegal to declare same variable twice in the same scope
  2. 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 with var. And because var variables have function scope instead of block scope, it is actually common to do this kind of redeclaration. The variable i is frequently used for integer values, and especially as the index variable of for loops. In a function with multiple for loops, it is typical for each one to begin for(var i = 0; .... Because var 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值