In this section, the target
- Learn how to declare variables.
- Learn the difference between var and let.
Content in this paper,
This article explains the two ways of variable declaration var and let, and compares the differences between var and let declaration variables, and finally summarizes the rules of variable name name.
Read for 10 to 15 minutes
Variable basis
Var and let keywords can be used to declare variables in js. Var is es5 syntax, let is ES6 syntax.
Var declares variables
Var can be used to declare local and global variables. The syntax is as follows:
Declaration: var variable name; Declare and assign: var variable name = value; Declare multiple variables: var variable 1, variable 2; Var variable 1 = value, variable 2 = value;Copy the code
Among them:
1. Variables can be thought of as containers for storing data. 2. Javascript is a weakly typed language. Values can be numbers, strings, arrays, etc.Copy the code
A simple example:
var a;
a = 1;
var i, j;
i = 8;
j = 9;
var x = 1, y = 2, z = 3;
Copy the code
Example 1: Given that the side length of the Rubik’s cube is 5.6cm, calculate the circumference and area of a single face and print it on the console.
var x = 5.6; // x is the length of the side
var c, a; // C is for perimeter and A is for interview
c = x * 4;
a = x * x;
console.log("The circumference of a single face of the Rubik's cube is:", c);
console.log("The single area of the Rubik's cube is:", a);
Copy the code
Example 2, given a sphere with radius R, takes the value entered by the user, calculates the cross section circumference and area, and prints it on the console.
var r; // r is the radius of the sphere
var c, a; // C is for perimeter and A is for interview
r = window.prompt("Please enter the radius of the ball in meters:");
c = 2 * Math.PI * r;
a = Math.PI * r * r;
console.log("Circumference:", c, "米");
console.log("Area:", a, "Square meters");
Copy the code
Variable naming rules
In addition, variable names have certain rules:
1. Variables must start with a letter and can also start with $or _. 2. Variable names are case-sensitive (y and y are different variables). 3. Variable names cannot be javascript keywords (e.g., var let function class, etc.).Copy the code
Think about:
The following does not comply with the variable name rule:
_this 4age while name10 $class let Score
Copy the code
Let declaration variable
Let can be used to declare block-scoped local variables. Variables already declared by let cannot be declared twice by var and let in the same scope.
The syntax is the same as var, except that the keyword is replaced with let:
Declaration: let variable name; Declare and assign: let variable name = value; Declare multiple variables: let variable 1, variable 2; Let variable 1 = value, variable 2 = value;Copy the code
A simple example:
let sum = 0;
let money = 9.9;
let i = 10, j, k;
j = 11;
k = 12;
Copy the code
The use of let is similar to that of var. Instead of giving an example, let’s talk about the difference between let and var. This is often asked in interviews.
Let and var:
1. Let variables cannot be redefined. Var variables can be redefined. 2. Let cannot access this variable before it is defined, but var can. 3. The scope is different. Var is the function scope and let is the block scope.Copy the code
Each of these differences is explained below.
1) Let variables cannot be redefined, whereas var variables can.
Variables declared by let, if declared again, will report an error.
let x = 1;
let x = 2; // This line will report an error
Copy the code
Error message:
Uncaught SyntaxError: Identifier 'x' has already been declared
Copy the code
That means the variable x is already defined.
Take a look at the var case:
var x = 1;
var x = 2;
Copy the code
Everything is normal.
2) Let declared variables cannot be accessed before definition, but var can.
Let must first declare variables to access, similar to C, Java, etc. Var can be used as a variable before it is declared, but it will be undefined before it is declared.
Let’s look at the let case first:
console.log(x);
let x = 1;
Copy the code
An error message is displayed when the following code is executed:
index.html:9 Uncaught ReferenceError: Cannot access 'x' before initialization
at index.html:9
Copy the code
This means that x needs to be declared before it can be accessed.
Take a look at the var case:
console.log(x); // undefined
var x = 1;
Copy the code
Run the code above to print the console:
undefined
Copy the code
Var x is promoted to the top of the list, but assignment is done at runtime.
So the above code is equivalent to:
var x;
console.log(x); // undefined
x = 1;
Copy the code
3) Different scope, let is block scope, var is function scope.
The scope of variables involves functions, prototype chains, and so on, which we will cover after the object-oriented section.
Since the use of let is more stringent and canonical, it is recommended that the let keyword be used for declaration.
This section summarizes
- Var and let can be used to declare variables in js.
- Variable naming requires certain rules, in addition to the need to see the name of the meaning.
exercises
- Please try to declare related variables to represent a video information, such as: video ID, video address, at least five, you can refer to the video application.
- Please try to declare related variables to represent a commodity information, such as: commodity ID, commodity name, at least 5, you can refer to the e-commerce application.
- Please try to declare related variables to represent an article information, such as: article ID, article title, at least 5, can refer to the news app.
- Please try to declare relevant variables to represent a comment information, such as: comment ID, comment content, at least 5, can apply the relevant comment function.
- Number_hits, temp99, function, credit, name, 9age, namecredit, _name, 9age, namecredit, name, 9age, name
- What is the difference between var and let declarations?
- How does variable promotion work?