This article is only for learning and sharing your own understanding
- Review the past and learn the new.
Judgment: if and ternary
In the first chapter, we talked about various operators, but the ternary operators are different from arithmetic operations, so they are not written in the same chapter.
If the else statement
Common statements are if else, if(…) Statement evaluates the conditional expression in parentheses, and if true, executes the corresponding code block. Chestnut:
Let test = true if(test) {alert(' in! '); } // If (test) alert(' in! ');Copy the code
If statements sometimes contain an optional “else” block. If the condition is not true, its internal code is executed. Chestnut:
Let test = true if(test) {alert(' in! '); } else {alert(' enter another door '); }Copy the code
Sometimes we need to test several variants of a condition. We can do this by using the else if clause.
Let test = true if(test) {alert(' in! '); } else if {alert(' enter second door '); }else {alert(' enter the third door '); }Copy the code
The above code blocks are carried out from top to bottom, strictly in accordance with the synchronization of JS, first judge if is true, if not, will proceed to the next statement judgment, successively down. Of course you can have more else if blocks. The trailing else is optional.
Type conversion
If (…). Statement evaluates the expression inside the parentheses and converts the result to a Boolean
- digital
0
, empty string""
,null
,undefined
和NaN
Will be converted tofalse
. Because they are called “falsy” values. - Other values are converted to
true
So they are called truthy.
The ternary operator (conditional operator) ‘? ‘
We need to assign a value to a variable based on a condition and we want to use that if else thing that we just talked about, but we feel like we don’t need to write a code block just to assign a value. So-called ternary operators allow us to do this much more quickly. It is called “triple” because there are three operands in the operator. It’s actually the only JavaScript operator with that many operands. How to use it? Chestnut:
let result = condition ? value1 : value2; // Evaluates the result of the condition, returning 'value1' if the result is true, or 'value2' otherwise.Copy the code
The ternary operator is not needed when the ternary operator is used to return Boolean. Chestnut:
let result = age > 18 ? true : false; // same as let result = age > 18Copy the code
Multiple ternary operators can also be nested, but only one value can be returned depending on multiple conditions. Chestnut:
let message = (age < 3) ? 'I'm under three' : (age < 18)? 'I'm under 18' : (age < 100)? 'I'm not a hundred' : 'That's all that's left of my age ';Copy the code
It can be hard to see what’s going on all at once. It affects readability.
- The first question mark check
age < 3
. - If true – returns
'I'm not three.'
. Otherwise, the colon continues":"
After the expression, checkage < 18
. - If true – returns
'I'm under 18'
. Otherwise, the next colon is continued":"
After the expression, checkage < 100
. - If true – returns
'I'm not 100.'
. Otherwise, the last colon continues":"
The following expression, returns'What's left is my age.'
.
Sometimes ternary operators are used for simplicity, but more can be complicated and it is not recommended.
Tips: It is recommended to wrap a block of code in braces {} every time you use an IF statement, even if there is only one statement. This improves code readability.
Logical operator
There are three common logical operators in js: | | (or), && (with),! (a). There are also new non-air operators:??
Although they are called “logical” operators, these operators can be applied to any type of value, not just Boolean values. Their results can also be of any type.
| | (or)
One or operation | | chain, will return to the first true value, if there is no true value, it returns the last value of the chain. Chestnut will explain this passage below.
Two vertical lines symbol | | “or” operator:
result = a || b;
Copy the code
When used for judgment purposes, there are four logical cases
alert( true || true ); // true
alert( false || true ); // true
alert( true || false ); // true
alert( false || false ); // false
Copy the code
Used for expressions or lists of variables
let firstName = ""; // let lastName = ""; // let nickName = "SuperCoder"; / / true value alert (firstName | | lastName | | nickName | | "Anonymous"); // SuperCoderCopy the code
The values of the preceding variables are false, resulting in “Anonymous”.
&& (with)
The and operation returns the first false value, or the last value if there is no false value. The above rule is very similar to the or operation. The difference is that the and operation returns the first false value, while the or operation returns the first true value.
The two ampersand symbols represent the && and operators:
result = a && b;
Copy the code
Unlike the or operator, which returns true only when all values are true, here are four chestnuts compared to the or operator:
alert( true && true ); // true
alert( false && true ); // false
alert( true && false ); // false
alert( false && false ); // false
Copy the code
! (a)
Exclamation sign! Represents a Boolean non operator.
In the first passage is not equal to (! ==) yes
A logical non-operator takes an argument and behaves as follows:
- To convert operands to Boolean types:
true/false
. - Return the opposite value.
Sometimes we see two!! Is used to convert a value to a Boolean type
?? (Non-air carrier operator)
With the (| |) or operator, the only different is only the duty is null or undefined only when false value, the rest is true.
It is possible to abbreviate?? In addition to the non-air carrier operator. = null assignment operator:
let x = null let y = 5 alert(x ?? = y) // => 5Copy the code
Broken down into:
let x = null
let y = 5
alert(x = (x ?? y)) // => 5
Copy the code
However, for security reasons, JavaScript forbids?? Operator with && and | | operators are used together, unless you use parentheses to specify the priority.
let x = 1 && 2 ?? 3; // Syntax error
Copy the code
Tips:
- priority
!
>&&
>||
??
Operators have very low precedence, only slightly higher?
和=
, so consider adding parentheses when using it in expressions.- If parentheses are not explicitly added, it cannot be equated with
||
或&&
Use together.
And for a while
A loop is a way to run the same code repeatedly.
while
cycle
While (condition) {// code // the so-called "body"}Copy the code
When condition is true, the loop body’s code is executed.
For example, the following loop outputs the value of I when I < 3:
let i = 0; While (I < 3) {// display 0, 1 and 2 alert(I); i++; }Copy the code
A single execution of the loop body is called an iteration. The loop in the example above has three iterations.
Without i++ in the example above, the loop (theoretically) repeats forever, called an “infinite loop.” Browsers provide a way to stop this loop by stopping the server-side JavaScript by terminating the process.
When the body of the loop has only one statement, the braces {… } :
let i = 3;
while (i) alert(i--);
Copy the code
for
cycle
The for loop is more complex, but it is the most commonly used form of loop.
The for loop looks something like this:
for (begin; condition; Step) {/ /... The loop body... }Copy the code
Chestnut:
for (let i = 0; i < 3; I ++) {// result 0, 1, 2 alert(I); }Copy the code
Analyzing the for loop:
begin | i = 0 |
Execute once upon entering the loop. |
---|---|---|
condition | i < 3 |
Check before each iteration of the loop and, if false, stop the loop. |
Body (circulatory body) | alert(i) |
If the condition is true, repeat the run. |
step | i++ |
Executes after each iteration of the loop body. |
The for loop algorithm works as follows:
Start running → (if condition is true → Run body and then run step) → (if condition is true → run body and then run step) → (If condition is true → Run body and then run step Step) - >...Copy the code
Apply this principle to chestnuts:
// for (let i = 0; i < 3; If (I < 3) {alert(I); if (I < 3) {alert(I); // If the condition is true, run the next step if (I < 3) {alert(I); // If the condition is true, run the next step if (I < 3) {alert(I); I++} / /... I'm done, because now I == 3Copy the code
Omit the statement
The for condition requires three conditions that can be omitted if they are not needed
let i = 0; // We have declared I and assigned it the value for (; i < 3; I ++) {// The "begin" statement section alert(I) is no longer needed; // 0, 1, 2}Copy the code
The begin argument is omitted, but; It has to exist. We can also remove the step statement segment:
let i = 0;
for (; i < 3;) {
alert( i++ );
}
Copy the code
This is equivalent to while (I < 3). Two for; Must exist, otherwise syntax errors will occur.
For also has statements:
break |
Force out of loop |
---|---|
continue |
break The “lightweight version” of the. It doesn’t stop the whole cycle. Instead, stop the current iteration and force a new cycle (if possible). |