1, Data type
JS divides data types into basic data types and complex data types
Basic data type:
Number, string, Boolean, undefined, null
Complex data types:
object
1. Digital type
The numeric type in JavaScript can be used to hold integers or floating-point numbers (decimals)
var age = 18; // integer var pi = 3.14; // Floating point number (decimal)
1.1 common hexadecimal
- Octal number: add 0 at the beginning of the number. The octal number is composed of 0 ~ 7 and is carried every 8
- Decimal number: it is a digital system based on 10. It is composed of 0 ~ 9 and carries every 10
- Hexadecimal number: add 0x at the beginning of the number. The hexadecimal number consists of 0-9 and a-f
1.2 range: maximum and minimum values of digital type
- Maximum value: number MAX_ Value, output result: 1.7976931348623157e+308
- Minimum value: number MIN_ Value, output result: 5e-324
1.3 special values
3 special values of digital type: Infinity and - Infinity and NaN:
-
Infinity: such as Number.MAX_VALUE*2
-
Infinity (infinitesimal): e.g. - Number.MAX_VALUE*2
-
NaN (non numerical): such as' abc '– 100
2. String type
String refers to a series of characters used to represent text in the computer. In JavaScript, single quotation marks or double quotation marks are used to wrap the string.
var str1 = 'Single quote string'; var str2 = "Double quoted string";
2.1 single and double quotation mark nesting:
Double quotation marks can be written directly in a single quotation string
Single quotation marks can also be written directly in double quotation string
var str1 = 'I am a "girl"'; // I am a "programmer" var str2 = "I'm a 'girl'"; // I'm a 'programmer' var str1 = 'I'm a girl'; // Single quotation mark incorrect usage var str2 = "I'm a "girl""; // Double quotation mark incorrect usage var str3 = 'I am a girl"; // Mixing single and double quotation marks
2.2 escape character
Escape character: when special symbols such as line feed and Tab are used in the string, escape character can be used for escape, and the escape character starts with "\".
Common escape characters and their descriptions are as follows:
Escape character | interpretative statement |
---|---|
\n | Newline, n means newline |
' | Single quotation mark |
" | Double quotation mark |
\t | tab indent |
2.3 string length
String length: a string is composed of several characters. The number of these characters is the length of the string. The length of the entire string can be obtained through the length property of the string.
var str1 = 'I\'m a girl'; console.log(str1.length);
The output result is:
2.4 accessing characters in strings
Accessing characters in the string: the string can use the "[index]" syntax to access characters according to index. Index starts from 0 until the length of the string minus 1. If the maximum value of index is exceeded, undefined will be returned.
var str = 'I\'m a girl'; console.log(str[0]); // Output result: I console.log(str[1]); // Output result: ' console.log(str[9]); // Output result: l console.log(str[10]); // Output result: undefined
The output results are shown in the figure:
2.5 string splicing
String splicing: multiple strings can be spliced with "+". If the data types are different, other types will be converted into strings before splicing, and then spliced into a new string.
var str = 'I\'m a girl'; console.log('a' + 'b'); // String addition console.log('a' + 18); // String + numeric console.log('_' + true); // String + Boolean console.log('12' + 14); // String + numeric console.log(12 + 14); // add magnitude
The results are as follows:
2.6 "show age" case
An input box pops up for the user to enter the age. After input, click "OK" button, and a warning box pops up. The content is "you are x years old this year", and X represents the age just entered.
var age = prompt('Please enter your age'); // An input box pops up for the user to enter the age var msg = 'You this year' + age + 'Years old'; // Concatenate the age with the output string alert(msg); // Pop up the warning box and output the processing results of the program
3. Boolean
Boolean is usually used for logical judgment. It has two values: true and false, indicating the "true" and "false" of things.
console.log(true); // Output result: true console.log(false); // Output result: false console.log(true + 1); // Output result: 2 console.log(false + 1); // Output result: 1
When Boolean and numeric types are added, the value of true is 1 and the value of false is 0.
4. undefined and null
4.1 if a variable is declared without assignment, the value of the variable is undefined. The following code demonstrates the use of undefined.
// Demonstrate the use of undefined console.log(true); // Output result: true var a; console.log(a); // Output result: undefined console.log(a + '_'); // Output result: undefined_ (string type) console.log(a + 1); // Output result: NaN
4.2 of course, assign a null value to a variable. The following code demonstrates the use of null value.
var b = null; console.log(b + '_'); // Output result: null_ (string type) console.log(b + 1); // Output result: 1 (b converted to 0) console.log(b + true); // Output result: 1 (b is converted to 0, and true is converted to 1)
2, Data type conversion
1. Convert to string
- Use "+" to splice strings (the most common way)
- Use toString() to convert to string
- Convert to string using String()
Note: null and undefined cannot be converted by toSting(); For numeric variables, you can pass in parameters in the parentheses of toString() to perform hexadecimal conversion.
<script> var num3 = 20; var str3 = console.log(num3 + ''); // +No. splice string var num1 = 18; var str1 = num1.toString(); // toString() method console.log(str1); console.log(typeof(str1)); var num2 = 19; str2 = String(num2); // String() method console.log(str2); console.log(typeof(str2)); </script>
2. Convert to numeric
- Use parseInt() to convert a string to an integer
- Use parseFloat() to convert a string to a floating point number
- Use Number() to convert a string to a number
- Implicit conversion using arithmetic operators (-, *, /)
<script> var str1 = '12'; var num1 = parseInt(str1); // parsetInt() console.log(typeof(num1)); console.log(num1); var str2 = '12.34'; var num2 = parseFloat(str2); // parseFloat() console.log(typeof(num2)); console.log(num2); var str3 = '12'; var num3 = Number(str3); // Number cast function console.log(typeof(num3)); console.log(num3); var str4 = '12'; var num4 = '12'-0; console.log(typeof(num4)); // Implicit conversion console.log(num4); </script>
The printing result is shown in the figure:
3. Convert to Boolean
Use Boolean() to convert to Boolean. During conversion, values representing null and negative will be converted to false, such as empty string, 0, NaN, null and undefined, and the rest will be converted to true.
<script> console.log(Boolean(NaN)); console.log(Boolean(null)); console.log(Boolean(undefined)); </script>
The print result is:
3, Operator
1. Arithmetic operator
Arithmetic operators are used to perform arithmetic operations on two variables or values. They are similar to mathematical addition, subtraction, multiplication and division. The commonly used arithmetic operators are as follows.
operator | operation | Example | interpretative statement |
---|---|---|---|
+ | plus | 1+5 | 6 |
- | reduce | 2-1 | 1 |
* | ride | 2*3 | 6 |
/ | except | 6/2 | 3 |
% | Remainder | 7%5 | 2 |
Precautions for arithmetic operators:
- When carrying out four mixed operations, the operation sequence shall follow the principle of "multiplication and division before addition and subtraction" in mathematics
- During modulo operation, the positive and negative of the operation result depends on the sign of the module (% the number on the left) and has nothing to do with the sign of the module (% the number on the right)
- In the development, try to avoid using floating-point numbers for operation, because the accuracy of JavaScript may lead to deviation of results
- Use "+" and "-" to represent positive or negative numbers
An expression is a collection of various types of data, variables and operators. The simplest expression can be a variable or literal.
Note: the expression will eventually have a return value.
2. Increment and decrement operators
Using the increment (+ +) and decrement (–) operators, you can quickly increment and decrement the value of a variable. It belongs to a unary operator and operates on only one expression.
- Pre increment (decrement) operator: increment and decrement operators are written in front of variables and return the calculated results
- Post increment (decrement) operator: increment and decrement operators are written after variables and return the results before calculation
- The increment and decrement operators take precedence over operators such as "+" "-"
3. Comparison operator
The comparison operator is used to compare two data. The result is a Boolean value, that is, true or false. See the following table for common comparison operators and usage.
Emphasize the use of the "=" operator
4. Logical operator
Logical operators are used to operate on Boolean values, and their return values are also Boolean values. In actual development, logical operators are often used to judge multiple conditions. The common logical operators are shown in the table below.
Logical operator | explain | case |
---|---|---|
&& | 'logic and' | true && false |
II | 'logical or' | true II false |
! | 'logical non' | !true |
5. Assignment operator
The assignment operator is used to assign the value on the right of the operator to the variable on the left. Common logical operators and examples are as follows.
console.log(Boolean('')) // false var age = 10; age += 5; // Equivalent to age = age + 5; console.log(age); // Output result: 15 age -= 5; // Equivalent to age = age - 5; console.log(age); // Output result: 10 age *= 10; // Equivalent to age = age * 10; console.log(age); // Output result: 100
6. Ternary operator
Ternary operator is an operator that requires three operands. The result of the operation is determined according to the given conditions.
Syntax format: conditional expression? Expression 1: expression 2
Syntax description: find the value of the conditional expression first. If it is true, the execution result of expression 1 will be returned; If the value of the conditional expression is false, the execution result of expression 2 is returned.
7. Operator priority
Operator priority: refers to the order in which all operators in the expression participate in the operation. The priority is shown in the following table.
end!!