Javascript if conditional judgment method summary

  • 2020-03-30 02:59:49
  • OfStack

Conditional statements are used to perform different actions based on different conditions.

Conditional statements

Often when writing code, you need to perform different actions for different decisions. You can use conditional statements in your code to do this.

In JavaScript, we can use the following conditional statements:

The & # 8226; If statement - this statement is used to execute code only if the condition is true
The & # 8226; The if... Else statement - executes code when the condition is true and other code when the condition is false
The & # 8226; The if... Else if... Else statement - use this statement to select one of several code blocks for execution
The & # 8226; Switch statement - this statement is used to select one of several code blocks for execution

If statement

The statement executes the code only if the condition is true.

grammar


if ( conditions )
  {
   Only if the condition is  true  Is the code that executes when 
  }

Note: use lowercase if. Using uppercase (IF) generates JavaScript errors!

The instance
When the time is less than 20:00, generate a "Good day" greeting:


if (time<20)
  {
  x="Good day";
  }

The result of x is:

Good day,

Try it yourself

Note that in this syntax, there is no.. The else.. . You have told the browser to execute the code only if the condition is true.
The If... Else statements
Please use the if... The else statement executes the code when the condition is true and the other code when the condition is false.

grammar


if ( conditions )
  {
   When the condition for  true  Is the code that executes when 
  }
else
  {
   When the condition is not  true  Is the code that executes when 
  }
  

The instance
When the time is less than 20:00, you will get the greeting "Good day", otherwise you will get the greeting "Good evening".


if (time<20)
  {
  x="Good day";
  }
else
  {
  x="Good evening";
  }
  

The result of x is:

Good day,

Try it yourself
The If... Else if... Else statements
Use the if... Else if... Else statement to select one of multiple code blocks for execution.

grammar


if ( conditions  1)
  {
   When the condition  1  for  true  Is the code that executes when 
  }
else if ( conditions  2)
  {
   When the condition  2  for  true  Is the code that executes when 
  }
else
  {
   When the condition  1  and   conditions  2  Don't for  true  Is the code that executes when 
  }

The instance
If the time is less than 10:00, the greeting "Good morning" will be sent; otherwise, if the time is less than 20:00, the greeting "Good day" will be sent; otherwise, the greeting "Good evening" will be sent:


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, what values can be used as conditions for if

1. Boolean variable true/false
2, the number is not 0, not NaN/ (0 or NaN)

In the following example, don't assume that a negative number means the if statement is false.


var i = -1;
if(i){
 alert('here');
}else{
 alert('test is ok!');
}

3. Object not null/ (null or undefined)
4. Non-empty string ("")/empty string ("")

To sum up, for strings, you don't have to write a whole bunch of if(STR! = null && STR! = undefined && STR! = "), just use one sentence


if(!str){
    //do something
}

That's it.

For non-null judgments of Numbers, consider using the isNaN() function, where NaN is not equal to any type of data, including itself, and can only be judged with isNaN(). For numeric types, if(a) is false when a is 0 in the if(a) statement, and true if(a) is not 0


var b;
var a = 0;
a = a + b;
if(a){
 alert('1');
}else{
 alert('2');
}
if(isNaN(a)){
 alert('a is NaN');
}

Javascript tutorial: method for if statement optimization

UglifyJS is a tool to compress and beautify javascript, and in its documentation I've seen several ways to optimize if statements. Although I haven't used it for some tentative testing yet, you can see here that it does make js look good. Some might think that the if statement is that simple, and how far can it be optimized? But look at the following ways and you may change your mind.

Use common ternary operators

If (foo) bar (); The else baz (); = = > Foo? Bar () : baz ();
If (! Foo) bar (); The else baz (); = = > Foo? Baz () : the bar ();
If (foo) return the bar (); The else return baz (); = = > Return foo? Bar () : baz ();

You are probably familiar with the use of the ternary operator above to optimize if statements, and you probably use it a lot.


<script>
var i=9
var ii=(i>8)?100:9;
alert(ii);
</script>

Output results:

100

Use and(&&) and or(||) operators

If (foo) bar (); = = > Foo && bar ();
If (! Foo) bar (); = = > Foo | | bar ();

To be honest, I haven't written code like this, which I saw when I was studying brother bird's Linux kitchen, but I didn't expect to implement it in js.

3. Omit curly braces {}

If (foo) return the bar (); The else something (); = = > {if (foo) return the bar (); Something ()}

This is familiar to you and me, but I recommend doing it while your code is optimized, or letting UglifyJS do it for you. After all, the code is not very readable without a brace.

At this point, I came up with a way for the father of jQuery to get attributes for HTML elements in JavaScript mastery.

The function getAttr (el, attrName) {
Var attr = {'for':'htmlFor', 'class':'className'}[attrName] || attrName;
};

If we don't, we'll probably need two if statements to do the processing, and the above code is not only clean and efficient, but also readable.

If you think about it, sometimes we can all find effective ways to solve our problems, but the key is whether we put our heart into finding a better way.

If (x==null

If (x==null) or if(typeof (x) == 'undefined') can be abbreviated as if(! X), not verified.

If (x) means that x is not null

Determines whether an object exists


if(document.form1.softurl9){
//Determine whether softurl9 exists to prevent js errors
}


if(document.getElementById("softurl9")){
//Determine whether softurl9 exists to prevent js errors
}


Related articles: