|
| 1 | +/* |
| 2 | +
|
| 3 | +What is a condition in code? |
| 4 | +Conditions are statements that are created by the programmer which evaluates actions in the program and evaluates if it's true or false. |
| 5 | +
|
| 6 | +JavaScript If-else |
| 7 | +
|
| 8 | +The JavaScript if-else statement is used to execute the code whether condition is true or false. There are three forms of if statement in JavaScript. |
| 9 | +
|
| 10 | + 1)If Statement |
| 11 | + 2)If else statement |
| 12 | + 3)if else if statement |
| 13 | +
|
| 14 | +1)if statement |
| 15 | +
|
| 16 | +It evaluates the content only if expression is true. The signature of JavaScript if statement is given below. |
| 17 | +
|
| 18 | + if(expression){ |
| 19 | + //content to be evaluated |
| 20 | + } |
| 21 | +
|
| 22 | +let age1 = 18; |
| 23 | +if (age1 >= 18) { |
| 24 | + console.log('You can sign up'); |
| 25 | +} |
| 26 | +
|
| 27 | +JavaScript If...else Statement=> |
| 28 | +
|
| 29 | +It evaluates the content whether condition is true of false. The syntax of JavaScript if-else statement is given below. |
| 30 | +
|
| 31 | + if(expression){ |
| 32 | + //content to be evaluated if condition is true |
| 33 | + } |
| 34 | + else{ |
| 35 | + //content to be evaluated if condition is false |
| 36 | + } |
| 37 | +
|
| 38 | +var a = 20; |
| 39 | +if (a % 2 == 0) { |
| 40 | + document.write("a is even number"); |
| 41 | +} |
| 42 | +else { |
| 43 | + document.write("a is odd number"); |
| 44 | +} |
| 45 | +
|
| 46 | +JavaScript If...else if statement |
| 47 | +
|
| 48 | +It evaluates the content only if expression is true from several expressions. The signature of JavaScript if else if statement is given below. |
| 49 | +
|
| 50 | +if(expression1){ |
| 51 | + //content to be evaluated if expression1 is true |
| 52 | + } |
| 53 | + else if(expression2){ |
| 54 | + //content to be evaluated if expression2 is true |
| 55 | + } |
| 56 | + else if(expression3){ |
| 57 | + //content to be evaluated if expression3 is true |
| 58 | + } |
| 59 | + else{ |
| 60 | + //content to be evaluated if no expression is true |
| 61 | + } |
| 62 | +
|
| 63 | +var b = 40; |
| 64 | +if (b > 40) { |
| 65 | + document.write("b is greayer than 30"); |
| 66 | +} |
| 67 | +else if (b < 40) { |
| 68 | + document.write("b is less than 40"); |
| 69 | +} |
| 70 | +
|
| 71 | +else if (b == 40) { |
| 72 | + document.write("b is equal to 40"); |
| 73 | +} |
| 74 | +
|
| 75 | +Introduction to the JavaScript switch case statement |
| 76 | +
|
| 77 | +The switch statement evaluates an expression, compares its result with case values, and executes the statement associated with the matching case value. |
| 78 | +
|
| 79 | +var grade = "A"; |
| 80 | +var res; |
| 81 | +
|
| 82 | +switch (grade) { |
| 83 | + case 'A': |
| 84 | + res = "grade A"; |
| 85 | + break; |
| 86 | +
|
| 87 | + case 'B': |
| 88 | + res = "grade B"; |
| 89 | + break; |
| 90 | +
|
| 91 | + case 'c': |
| 92 | + res = "grade c"; |
| 93 | + break; |
| 94 | +
|
| 95 | +
|
| 96 | + default: |
| 97 | + res = 'null'; |
| 98 | + break; |
| 99 | +} |
| 100 | +
|
| 101 | +document.write(res); |
| 102 | +
|
| 103 | +
|
| 104 | +*/ |
0 commit comments