top of page
Writer's picture_Romeyo Boy_

JS : else Statement



OK, so we've seen that when the code block evaluates to false using the if statement, the action is skipped, but what if we want to do something else.


Well, we definitely use the "else" statement!


We can use the else statement to specify a block of code that will be executed if the condition is false. Which are exactly like this:


<> Code Syntax :

Conscious! - You can omit the curly braces if there is only one command in the code under the condition.



Here's another example of if and else statements are working together:


<> Code Example :

Let's translate this example and try to understand it:

  • If myNum1 is greater than myNum2, alert "This is my first condition";

  • Otherwise, alert "This is my second condition";


So the browser will print out the second condition as 7 which is not greater than 10.


Conscious! - Another way to use this check is with the question mark (?) operator: a > b? alert(a): alert(b)






2 views0 comments

Recent Posts

See All

Comments


bottom of page