What are the Conditional Statements in #JavaScript?
Thread🧵
🟡What is a Conditional Statement?
Very often when you write code, you want to perform different actions for different decisions.
Conditional statements control behavior in JavaScript and determine whether or not pieces of code can run.
🟡We use "if" to specify a block of code to be executed, if a specified condition is true.
🟡We use "else" to specify a block of code to be executed, if the same condition is false
🟡We use "else if" to specify a new condition to test, if the first condition is false.
🟡1) If Statement:
As mentioned before, the if statement only runs if the condition enclosed in parentheses () is true.
Example:
In the above example, the output will be "if block".
💡(8 > 4) is the condition to test, which in this case it is true.
The part contained inside curly braces {} is the block of code to run.
Because the condition passes, the variable outcome is assigned the value "if block".
🟡2) Else Statement:
You can extend an if statement with an else statement, which adds another block to run when the if conditional doesn’t pass.
In the example above, if the hour is less than 18, the output will be "Good day", otherwise it will be"Good evening".
🟡3) Else If Statement:
This statement is to specify a new condition if the first condition is false.
In the above example, we're saying that - If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a "Good day" greeting, otherwise create a "Good evening".
That's all👊
If you’re a beginner and found this short thread useful, please consider following @salimelliye and retweeting the first tweet.