Home > Article > Web Front-end > What are the 4 ways to declare variables in javascript
Four ways to declare variables in JavaScript: 1. Use "var" to declare variables, such as "var a;"; 2. Use "function" to declare variables, such as "function Fun (num) {}"; 3. Use "let" to declare variables; 4. Use "const" to declare variables.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
There are several ways to declare variables in JavaScript:
Let’s verify it
Verification method one:
function repeatFun (num) { return num; } repeatFun(10); // 10 var repeatFun = 1; repeatFun(2); // Uncaught TypeError: repeatFun is not a function
This method uses var to repeatedly declare variables, but the latter will overwrite the former feature
Let’s take a look at what happened here:
According to the execution results, we can infer that there is a repeatFun variable in the browser's memory. It was a function before and was later redeclared by a var keyword and initially changed to 1.
Verification method two:
{ let repeatFun = 1; function repeatFun (num) { return num } } // Uncaught SyntaxError: Identifier 'repeatFun' has already been declared
The second method is to use a syntax of
ES6: using the feature of let that cannot be declared repeatedly to prove that function is also a declared variable The difference between
Variable declaration promotion
Repeated declaration
scope The scope of
The special thing about const
Example 1, verify var variable promotion:
var b = a + 1; // b: NaN var a = 1; // a: 1
First, declare it A variable b is initially recognized, and the initialized value is a 1 (what is the value of a?) <br> Then a variable a is declared, and the initial recognition is 1 <br> This is what the code looks like on the surface When doing these things, what is actually done is this:
The following writing method can also achieve the same effect
var b; var a; b = a +1; // b: NaN a = 1; // a: 1
let and const behave differently from var
Example 2, verify whether there is variable promotion in let:
let b = a + 1; // Uncaught ReferenceError: a is not defined let a = 1;
A scope error will be thrown directly when running. If you change it like this, There is no error:
let a = 1; // a: 1 let b = a + 1; // b: 2
const and let perform the same in terms of variable promotion
let and const cannot be declared repeatedly
Example 1, verify the repeated declaration of var:
var a = 1; var a = 2; var b = a + 1; // 3
Example 2, verify the repetition of let Statement:
let a = 1; let a = 2; // Uncaught SyntaxError: Identifier 'a' has already been declared
var a = 1; let a = 2; //Uncaught SyntaxError: Identifier 'a' has already been declared
encapsulates a factorial function as an example, without using tail recursion, just use for Implementation with if <br> Example 1, factorial function verifies the scope:
var num = 5; function factorial(num) { var result = 1,resultValue = 0; for (let i = num - 1; i >= 1; i--) { if (i === num - 1) { resultValue = num * i; }else{ resultValue = num * i / num; } result *= resultValue; } // i 是用 let 进行定义它的作用域仅仅被限制在 for 循环的区域内 // i++;// Uncaught ReferenceError: i is not defined return result; } // result 是用 var 进行定义,他的活动区域在 factorial 函数内 // result++; // var的作用域.html:34 Uncaught ReferenceError: result is not defined factorial(num); // 120
const behaves the same as let in the scope
Example 2, verifies const Scope:
{ const NUM_1 = 10; } let b = NUM_1 + 1; // Uncaught ReferenceError: NUM_1 is not defined
Example 3, verify that var can define global variables, let and const can only define local variables
// 可以挂载到全局作用域上 // var name = 'window scoped'; let name = 'let scoped'; //是不挂载到全局作用域中 let obj = { name: 'myName', sayName () { return function () { console.log(this.name); // 打印出来为空 }; } } obj.sayName()(); console.log(window); //name 这个属性的值没有,如下图
若这样改一下就可以得到我们想要的值:
这个同时也涉及到新问题 this 的指向。后面的文章再详细举例验证
const 与 let , var 其实还是有些地方不一样的
例子1:验证 const 的特殊之处(一)<br>
const NUM = 100; NUM = 1000; // Uncaught TypeError: Assignment to constant variable
但是也有例外
例子二:验证 const 的特殊之处(二)
const obj = { name: 'xiaoMing', sayName () { return this.name } }; obj.sayName(); // xiaoMing obj.name = 'xiaoHong'; obj.sayName(); // xiaoHong
若这样改一下: <br> 例子三:验证 const 的特殊之处(三)
const obj = { name:'xiaoMing', sayName(){ return this.name } }; obj = {}; // Uncaught TypeError: Assignment to constant variable
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of What are the 4 ways to declare variables in javascript. For more information, please follow other related articles on the PHP Chinese website!