JS writing position:
1. The inline expression is written directly inside the element
2. Embedded js
3. External JS < script SRC = "my. JS" > < / script >
JS notes:
Single line note//
Multiline comment shift+alt+a
JS input / output statement:
prompt('This is an input box') alert('Pop up warning box')
Variable:
Variable initialization var myname = 'zxy name'
(the variable value is a number and can not be quoted)
Update variable myname = 'zxy not up to baa'
Special case of declaring variables
Only declare that the result of no assignment is undefined, and no error is reported. Direct assignment without declaration is allowed
Swap two variable values:
A temporary variable is required
<script> var temp; var a1 = 1; var a2 = 2; temp = a1; a1 = a2; a2 = temp; </script>
Data type of variable:
js is a dynamic language, and the data types of variables can be changed
var x = 10; x = 'zxy'
Digital Number:
Add 0 before octal digit and add 0 in hexadecimal digit#
The maximum value of numeric type is number MAX_ VALUE
1.7976931348623157e+308
The minimum value of numeric type is number MIN_ VALUE
5e-32
Infinity
Infinitesimal Infinity
Non digital NaN
isNaN() this method is used to judge non numbers. It is a number false, not a number true:
console.log(isNaN(12)); //false console.log(isNaN('zxy')); //true
String type:
String nested outer double inner single, outer single inner double
String escape character
String length
String splicing + sign summary formula: numerical values are added and characters are connected
Splicing reinforcement
Boolean undefined Null:
A variable that has not been assigned a value after declaration will have a default value of undefined (pay attention to the result when connecting or adding)
A declared variable is given a null value, and the value stored in it is null (when learning objects, we continue to study null)
var variable; console.log(variable); // undefined console.log('Hello' + variable); // Hello, undefined console.log(11 + variable); // NaN console.log(true + variable); // NaN
var vari = null; console.log('Hello' + vari); // Hello, null console.log(11 + vari); // 11 console.log(true + vari); // 1
Get the data type of the variable:
console.log(typeof type)
Convert to character type:
1. Convert numeric type to string type variable toString()
2. Use string (variable)
3. ! Implicitly convert console. Using + concatenated string log(num + '')
Convert to digital:
1.! parseInt('') gets an integer
4. ! parseFloat('') gets decimal and floating point numbers
5. Use number (variable)
6. Use arithmetic operation to implicitly convert console log('12'-0)
Simple adder:
var num1 = prompt('Please enter the first value:'); var num2 = prompt('Please enter the second value:'); var result = parseFloat(num1) + parseFloat(num2); alert('Your result is:' + result);
Convert to Boolean:
console.log(Boolean('')); // false console.log(Boolean(0)); // false console.log(Boolean(NaN)); // false console.log(Boolean(null)); // false console.log(Boolean(undefined)); // false
Operator:
- The highest precision of floating-point values is 17 decimal places
// There will be problems in floating-point arithmetic console.log(0.1 + 0.2); // 0.30000000000000004 console.log(0.07 * 100); // 7.000000000000001 //We can't directly compare whether the floating-point numbers are equal var num = 0.1 + 0.2; console.log(num == 0.3); // false
- Increment and decrement operators
Post: original value operation before self addition (others before yourself)
Preposition: self addition first, then operation (already before person)
The expression returns the result
var e = 10; var f = e++ + ++e; // 1. e++ = 10 e = 11 2. e = 12 ++e = 12 console.log(f); // 22 // The subsequent self increment expression returns the original value, and the subsequent variable is self incremented by 1
-
Comparison operator
==Judge whether the values on both sides are equal. At this time, there is implicit conversion
===Judge whether the values and data types on both sides are exactly the same -
Logical operator
Short circuit operation: when there are multiple expressions (values) and the expression value on the left can determine the result, the operation of the expression value on the right will not be continued;
Logic and syntax: expression 1 & & expression 2
If the value of the first expression is true, expression 2 is returned
If the value of the first expression is false, the expression 1 is returned
css console.log( 123 && 456 ); // 456
console.log( 0 && 456 ); // 0
Logical or: Syntax: expression 1 | expression 2
If the value of the first expression is true, the expression 1 is returned
If the value of the first expression is false, expression 2 is returned
console.log(123 || 456); // 123
console.log(0 || 456 || 456 + 123); // 456 -
Assignment Operators
var age = 10; age += 5; // Equivalent to age = age + 5; age -= 5; // Equivalent to age = age - 5; age *= 10; // Equivalent to age = age * 10;
- Operator priority
The logical non priority in unary operators is very high
Logical and have higher priority than logical or
Process control:
Sequential execution, conditional execution (if, ternary expression, switch), loop execution (for, while, do while)
{conditional execution
Ternary expression: expression 1? Expression 2: expression 3;
If expression 1 is true, the value of expression 2 is returned; if expression 1 is false, the value of expression 3 is returned
Simple understanding: it is similar to the abbreviation of if else.
switch expression: when executing the statement in the case, if there is no break, continue to execute the statement in the next case.
In general, if... else... And switch... case statements can be replaced with each other: Switch... case statements are usually processed
Case is the case of comparing certain values, while the if... else... Statement is more flexible and is often used to judge the range (greater than or equal to a certain range) switch
The conditional statement of the program is directly executed after the conditional judgment of the statement, which is more efficient. If... else statements have several conditions, you have to judge how many times. When there are fewer branches, if
The execution efficiency of else statement is higher than that of switch statement. When there are many branches, the execution efficiency of switch statement is higher and the structure is clearer.
}
{loop execution
(dual) for loop
//Print n rows and N columns ⭐ var r = prompt('Number of rows:'); var c = prompt('Number of columns:'); var str = ''; for(var i = 1;i <= r;i++){ for(var j = 1;j <= c;j++){ str = str + '⭐'; } str += '\n'; } console.log(str);
var str = ''; for (var i = 1; i <= 10; i++) { // Outer loop control rows for (var j = i; j <= 10; j++) { // The number of inner layer cyclic printing is different, j = i str = str + '★'; } str += '\n'; } console.log(str);
// multiplication table // There are 9 lines in total, but the number of each line is different, so a double for loop is required // The for loop of the outer layer controls the number of lines i, which can be printed 9 times // The inner for loop controls each line of formula j // Core algorithm: the number of formulas in each line is exactly the same as the number of lines, J < = I; // After printing each line, you need to change another line var str = ''; for (var i = 1; i <= 9; i++) { // Outer loop control rows for (var j = 1; j <= i; j++) { // Inner layer loop controls the number of each line j < = I // 1 × 2 = 2 // str = str + '★'; str += j + '×' + i + '=' + i * j + '\t'; } str += '\n'; } console.log(str);
The difference between the while loop and the for loop is that the while loop can judge more complex conditions, such as user name and password
(do)while loop
// while loop case // 1. Print one's life, from 1 year old to 100 years old var i = 1; while (i <= 100) { console.log('This man this year' + i + 'Years old'); i++; } // 2. Calculate the sum of all integers between 1 and 100 var sum = 0; var j = 1; while (j <= 100) { sum += j; j++ } console.log(sum); // 3. Pop up a prompt box, do you love me? If you enter I love you, you will be prompted to end. Otherwise, keep asking. var message = prompt('Zhang Xinyao, will you marry me'); while (message !== 'marry') { message = prompt('Zhang Xinyao, will you marry me'); } alert('I Do!!!');
// do while loop case // 1. Print one's life, from 1 year old to 100 years old var i = 1; do { console.log('This man this year' + i + 'Years old'); i++; } while (i <= 100) // 2. Calculate the sum of all integers between 1 and 100 var sum = 0; var j = 1; do { sum += j; j++; } while (j <= 100) console.log(sum); // 3. Pop up a prompt box, do you love me? If you enter I love you, you will be prompted to end. Otherwise, keep asking. do { var message = prompt('Zhang Xinyao, will you marry me?'); } while (message !== 'marry') alert('I love you so much');
Continue keyword exit this time (current cycle) and continue to execute the remaining cycles;
The break keyword is used to immediately jump out of the entire loop (the end of the loop).
}
Naming conventions:
The naming of variables and functions must be meaningful
Variable names are generally nouns
The names of functions are usually verbs