Conditional statements are used to perform different actions based on different conditions.
Conditional statement
Usually when writing code, you always need to perform different actions for different decisions. You can use conditional statements in your code to complete the task.
In JavaScript, we can use the following conditional statement:
If statement-use this statement to execute code only if the specified condition is true
If...else Statement-executes code when the condition is true, and executes other code when the condition is false
if...else If....else Statement-Use this statement to select one of several code blocks to execute
Switch Statement-Use this statement to select one of several code blocks to execute
If statement
The statement executes code only if the specified condition is true.
Grammar
Copy Code code as follows:
if (condition)
{
Code that executes only if the condition is true
}
Note: Please use the lowercase if. Using uppercase letters (IF) generates JAVASCRIPT errors!
Instance
When the time is less than 20:00, generate a "Good day" greeting:
Copy Code code as follows:
if (time<20)
{
x= "Good Day";
}
The result of X is:
Good Day
Give it a shot yourself.
Please note that in this syntax, there is no. else ... You have told the browser to execute code only if the specified condition is true.
If...else statement
Use the If....else statement to execute code when the condition is true, and execute other code when the condition is false.
Grammar
Copy Code code as follows:
if (condition)
{
Code executed when the condition is true
}
Else
{
Code executed when the condition is not true
}
Instance
When the time is less than 20:00, the greeting will be "Good day", otherwise it will be greeted with "good evening".
Copy Code code as follows:
if (time<20)
{
x= "Good Day";
}
Else
{
x= "Good evening";
}
The result of X is:
Good Day
Give it a shot yourself.
If...else If...else Statement
Use the If....else if...else statement to select one of several code blocks to execute.
Grammar
Copy Code code as follows:
if (condition 1)
{
Code executed when condition 1 is true
}
else if (condition 2)
{
Code executed when condition 2 is true
}
Else
{
Code executed when both condition 1 and Condition 2 are not true
}
Instance
If the time is less than 10:00, the greeting "Good Morning" is sent, otherwise if the time is less than 20:00, the greeting "Good Day" is sent, otherwise the greeting "Good Evening" is sent:
Copy Code code as follows:
if (time<10)
{
X= "Good Morning";
}
else if (time<20)
{
x= "Good Day";
}
Else
{
x= "Good evening";
}
The result of X is:
Good morning
In JavaScript, which values can be used as an if condition?
1. Boolean Variable True/false
2, the number is not 0, not nan/(0 or Nan)
See the example below to think of negative numbers as if statements are false.
Copy Code code as follows:
var i =-1;
if (i) {
Alert (' here ');
}else{
Alert (' Test is ok! ');
}
3, the object is not null/(null or undefined)
4. String not Empty strings ("")/Empty Strings ("")
To sum up, for strings, do not write a large stack of if (str!=null && str!=undefined && str!= "), just use one sentence
Copy Code code as follows:
if (!STR) {
Do something
}
It's OK.
For non-null judgments of numbers, consider using the isNaN () function, which is not equal to any type of data, including itself, and can only be judged by isNaN (). For a numeric type, A is 0 o'clock if (a) is false in the IF (a) statement, not 0 o'clock if (a) is true
Copy Code code as follows:
var b;
var a = 0;
A = a + B;
if (a) {
Alert (' 1 ');
}else{
Alert (' 2 ');
}
if (isNaN (a)) {
Alert (' A is NaN ');
}
JavaScript Tutorial: About if Statement optimization method if shorthand
UGLIFYJS is a tool for compressing and beautifying JavaScript, and in its documentation, I've seen several ways to optimize if statements. Although I have not used it to do some experimental testing, but from here I can see that it does make JS a landscaping work. Perhaps some people think that if statement is so simple, can optimize to what extent? But if you look at the following ways, you may change your view.
First, use the common ternary operator
if (foo) bar (); else Baz (); ==> Foo?bar (): Baz ();
if (!foo) bar (); else Baz (); ==> Foo?baz (): Bar ();
if (foo) return bar (); else return Baz (); ==> return Foo?bar (): Baz ();
For the above use ternary operator to optimize the IF statement you are certainly not unfamiliar, perhaps you often use it.
Examples given by the cloud-dwelling community:
Copy Code code as follows:
<script>
var i=9
var ii= (i>8)? 100:9;
Alert (ii);
</script>
Output results:
100
II. Use and (&&) and OR (| |) Operator
if (foo) bar (); ==> Foo&&bar ();
if (!foo) bar (); ==> foo| | Bar ();
To be honest, I didn't write code like this, I've seen it when I was studying "brother Bird's Linux private dish", but I didn't expect it to happen in JS.
Three, omit curly braces {}
if (foo) return bar (); else something (); ==> {if (foo) return bar (); something ()}
You and I are familiar with this type of writing, but I suggest doing it in code optimization, or handing it over to Uglifyjs to help you solve it. After all, less than a curly brace, the code is not very readable.
Writing here, I think of the way in which the father of jquery is getting HTML element attributes in "proficient in JavaScript."
function GetAttr (el, Attrname) {
var attr = {' for ': ' Htmlfor ', ' class ': ' ClassName '}[attrname] | | Attrname;
};
If we do not write this, we may need to use two if statements for processing, and the above code is not only simple and effective, but also strong readability.
To think about it, we can all find an effective way to solve the problem, but the key is whether we find a better way by heart.
"JavaScript Tips" if (x==null) shorthand
if (x==null) or if (typeof (x) = = ' undefined ') can be abbreviated to if (!X) and not validated.
Conversely if (x) means X is not empty
Determine if an object exists
Copy Code code as follows:
if (DOCUMENT.FORM1.SOFTURL9) {
To determine whether there are softurl9 to prevent JS error
}
Copy Code code as follows:
if (document.getElementById ("Softurl9")) {
To determine whether there are softurl9 to prevent JS error
}