[Web front-end basics | JS basics] Variables

Insert picture description here

One: JavaScript input and output statements

In order to facilitate the input and output of information, JS provides some input and output sentences, the commonly used sentences are as follows:

method Description
alert(msg) Browser pop-up warning box
console.log(msg) Browser console print output information
prompt(info) The browser pops up an input box, the user can input
  • Note: alert() is mainly used to display messages to users, console.log() is used to show runtime messages to programmers.

Two: Variable

1: The basic concept of variables

[Talking Sketch]
4

2: Use of variables

2.1: Declare variables

//  声明变量  
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 allocated space in the memory through the variable name

2.2: 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

2.3: Output results

console.log(age);//输出的结果可以在控制台看到

3: Initialization of variables

var age  = 18;  // 声明变量同时赋值为 18   
  • No need to add "" to the number;
  • Add "" for other types;
var uname ="zhao";

4: Variable syntax expansion

4.1: Update variables

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.

5

4.2: Declare multiple variables at the same time

6

4.3: Special cases of declaring variables

description the reason Output result
Only declare, not assign Undefined undefined
Do not declare, do not assign, use directly Report an error not undefined
No declaration, only assignment Can be used, but not recommended

5: Variable naming convention

  • Composed of letters (A-Za-z), numbers (0-9), underscore (_), dollar sign ($)
  • Strictly case sensitive
  • Cannot start with a number
  • Cannot be keywords, reserved words
  • Variable name must be meaningful
  • Observe the camel case nomenclature. The first letter is lowercase, and the first letter of the following word needs to be uppercase. myFirstName

7
[Talking]
Supplement:

Identifier

Identification (zhi) symbol: refers to the name given by the developer for variables, attributes, functions, and parameters. Identifiers cannot be keywords or reserved words.

Keyword

Keywords: refers to the words that JS itself has used, and they can no longer be used as variable names and method names. Keywords are used to indicate the beginning or end of a control statement, or to perform specific operations
including: break, case, catch, continue, default, delete, do, else, finally, for, function, if, in, instanceof, new, return, switch, this, throw, try, typeof, var, void, while, with, etc.

Reserved word

Reserved words: In fact, they are reserved "keywords", which means that although they are not keywords now, they may become keywords in the future, and they cannot be used as variable names or method names.
Including: boolean, byte, char, class, const, debugger, double, enum, export, extends, fimal, float, goto, implements, import, int, interface, long, mative, package, private, protected, public, short, static, super, synchronized, throws, transient, volatile, etc.

If you use a reserved word as a variable name or function name, you may not receive any error messages unless the reserved word is implemented by future browsers. When the browser implements it, the word will be regarded as a keyword, so a keyword error will occur.

What are the JavaScript keywords? ? ?

JavaScript reserved keywords (novice tutorial)

Example: sum1=10; sum2=20, exchange the values ​​of two variables

 //交换两个的值
 var sum1 = 10, sum2 = 20, sum3;//sum3 为临时变量
 sum3 = sum2;
 sum2 = sum1;
 sum1 = sum3;
 console.log(sum1, sum2);

Guess you like

Origin blog.csdn.net/qq_43490212/article/details/111169473