A, if statements
Example 1:
// full Save 30 15 var . Price = 35 ; IF (. Price> 30 ) { . Price =. Price - 15 ; } the console.log ( 'should pay' + price);
Results: 20
Example 2:
// declare variables stored Age, if the age is greater than 60, then reduction based on the original 5, the age of the final print var Age = 61 is ; IF (Age> 60 ) { Age = Age - 5 ; } the console.log (Age)
Results: 56
Example 3:
// declare two variables were stored username and password, if a user name is root, and the password is 123456, print successful login; var username = 'root' ; var password = '123456' ; IF (username = 'root' && password = '123456' ) { the console.log ( 'Success' ); }
// writing logic short
(username = 'root' && password = '123456') && console.log ( 'success')
Result: success
Example 4:
// If the age is not less than 18 years old, print "adult", otherwise print "minor" // Note: If the statement after if only one row, it can omit the braces var Age = 21 ; if (Age> = 18 is ) { the console.log ( 'adult' ); }
results: adults
// if the conditional expression statement, some value defaults to false
0 null NaN underdined
Two, if_else statement
Example 1:
// If the cope 27, the balance enough, successful payment, or Tip: insufficient funds, please recharge var Money = 50 ; IF (Money> = 27 ) { the console.log ( 'successful payment' ); } the else { the console.log ( 'insufficient funds, please recharge!' ); }
result: successful payment
Example 2:
// cope var MONEY1 = 27 ; // balance var money2 = 25 ; IF (money2> = MONEY1) { the console.log ( 'successful payment' ); } the else { the console.log ( 'insufficient funds, please recharge!' ) ; }
result: the balance is insufficient, please recharge!
Example 3:
// declare personalized signature, if there is content, print, or print "This guy is lazy" var Signature = '' ; IF (Signature == '' ) { console.log ( 'This guy is lazy' ); } the else { Console, log (Signature); }