(JavaScript learning record): Variable

table of Contents

Variable overview

Use of variables

Declare variable

Assignment

Initialization of variables

Case 1

Case 2

Variable syntax expansion

Update variable

Declare multiple variables at the same time

Special cases of declaration variables

Variable naming convention

Case: Exchange the values ​​of two variables

summary


Variable overview

What is a variable

  • Vernacular: A variable is a box that contains things.
  • Popular: Variables are containers for storing data . We variable name to get data, even data can be modified.

  • Storage of variables in memory

    • Essence: A variable is a space for storing data that the program applies for in the memory .
    • Similar to our hotel rooms, a room can be regarded as a variable.

Use of variables

  • There are two steps when using variables:
    • 1. Declare variables 
    • 2. Assignment

Declare variable

// 声明变量 
var age; // 声明一个 名称为age 的变量
  • Var is a JS keyword used to declare variables (variable means variable).
    • After using this keyword to declare a variable, the computer will automatically allocate memory space for the variable, without the need for the programmer to manage
  • age is the variable name defined by the programmer, we need to access the space allocated in the memory through the variable name

Assignment

age = 10; // 给 age 这个变量赋值为 10
  • = Is used to assign the value on the right to the variable space on the left. This represents the meaning of assignment
  • The variable value is the value saved by the programmer into the variable space

Initialization of variables

var age = 18; // 声明变量同时赋值为 18
  • Declaring a variable and assigning a value, we call it the initialization of the variable.

Case 1

<script>
    // 1. 声明了一个age 的变量 
    var age;
    // 2. 赋值  把值存入这个变量中
    age = 18;
    // 3. 输出结果 
    console.log(age);
    // 4. 变量的初始化 
    var myname = 'pink老师';
    console.log(myname);
</script>

Case 2

  • demand:
    • 1. An input box pops up, prompting the user to enter a name.
    • 2. A dialog box pops up to output the name the user just entered.
<script>
    // 1. 用户输入姓名  存储到一个 myname的变量里面
    var myname = prompt('请输入您的名字');
    // 2. 输出这个用户名
    alert(myname);
</script>

Variable syntax expansion

Update variable

  • After a variable is re-assigned, its original value will be overwritten, and the value of the variable will be subject to the value of the last assignment .
//  更新变量
var myname = 'pink老师';
console.log(myname);
myname = '迪丽热巴';
console.log(myname);

Declare multiple variables at the same time

  • When declaring multiple variables at the same time, you only need to write one var, and use commas to separate multiple variable names .
// 声明多个变量
// var age = 18;
// var address = '火影村';
// var gz = 2000;
var age = 18,
    address = '火影村',
    gz = 2000;

Special cases of declaration variables

//  声明变量的特殊情况
//  只声明不赋值 结果是?  程序也不知道里面存的是啥 所以结果是 undefined  未定义的
var sex;
console.log(sex); // undefined
// 不声明 不赋值 直接使用某个变量会报错滴
console.log(tel);
//  不声明直接赋值使用
qq = 110;
console.log(qq);

Variable naming convention

  • Composed of letters (A-Za-z), numbers (0-9), underscores (_), dollar signs ($ ), such as: usrAge, num01, _name
  • Strictly case sensitive. var app; and var App; are two variables
  • Cannot start with a number. 18age is wrong
  • Cannot be keywords or reserved words. For example: var, for, while
  • The variable name must be meaningful. MMD BBD nl → age 
  • Observe the camel case nomenclature. The first letter is lowercase, and the first letter of the following word needs to be uppercase. myFirstName

Case: Exchange the values ​​of two variables

  • Requirements: exchange the values ​​of two variables (implementation idea: use a temporary variable for intermediate storage)
var temp; // 声明了一个临时变量
var apple1 = '青苹果';
var apple2 = '红苹果';
temp = apple1; // 把右边给左边
apple1 = apple2;
apple2 = temp;
console.log(apple1);
console.log(apple2);

summary

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/108582299