1. The use of variables
The essence of variables: a variable is a space that a program applies for in memory to store data
1. Declare variables
var variable name;
For example:
var=age;
2. Assignment
variable name = value
For example:
age=10;//给age这个变量赋值为10
3. Variable initialization
Declaring a variable and assigning a value is called variable initialization
For example:
var age=18;//声明变量同时赋值为18
4. Variable update
- After a variable is reassigned, its original value will be overwritten, and the variable value will be based on the last assigned value
For example:
Although updated, the original variable value will still be displayed
5. Declare multiple variables at the same time
When declaring multiple variables at the same time, you only need to write one var, and use English commas to separate multiple variable names.
For example:
var age=10,name='sy',sex=0;
6. Special cases for declaring variables
7. Variable naming rules
2. Data type
digital
1. Digital Number
Browsers convert to decimal by default
2. Digital range
JavaScript heavy value maximum and minimum values
3. Numeric special values
alert(Infinity);//Infinity,代表无穷大,大于任何值
alert(-Infinity);//-Infinity,代表无穷小,小于任何值
alert(NaN);//NaN,not a number,代表是一个非数值
4.isNaA()
Used to determine whether a variable is a non-numeric type, return true or false
- If it is a number, return false
- Returns true if not a number
string type String
1. String format
2. Nesting of string quotes
JS can nest double quotes with single quotes, or nest single quotes with double quotes (outer double inner single, outer single inner double)
for example:
3. String escape character
For example:
4. String length
A string is composed of several characters, the number of these characters is the length of the string, and the length of the entire string can be obtained through the length property of the string.
For example:
var str= 'hello,world!';
alert(str.length);//结果显示为12
5. String concatenation
- Multiple strings can be spliced using +, and the splicing method is string + any type = new string after splicing
- Before splicing, any type added to the string will be converted to a string, and spliced into a new string
For example:
The formula for the plus sign is: adding values and connecting characters
Boolean Boolean
Undefined and Null
Get the data type of the detection variable
Use typeof to get the data type of the detected variable
For example:
The number entered by the prompt is also a string type, which is the same as python's input
3. Data type conversion
1. Convert to string
For example: