Let's Talk About Conditionals
Think of conditionals like a storyline in a decision-based game: if you pick a certain response, something specifically happens. But you won't be able to go back and see what would've happened if you had chosen different. That's exactly how conditionals work in JavaScript.
Conditional Statements
Conditional statements are control structures in JavaScript that determine whether a specific part of our code should run. There are multiple conditions you can use in your code. Let me give you a visual representation on what conditionals look like.
As you can see in the picture above, you have two pathways: one path is if the condition is true, and the other path is for when the condition is false. Again, everything depends on what input is provided for the condition.
Let's look at an example.
Is the variable 'age' bigger than 'ageMin'?
What do you think will log to the console? You should see "You can enter the party!". The condition that we tested is true, and because it was true, only the code inside of that specific code block will run.
Types of Conditionals
In the previous example, you saw how a conditional works. That specific conditional is called an If...else statement. It basically says 'If this is true, run this code. If not, run this code instead.'.
There are other types of conditionals you can use as well. To build on to the If...else statement, if you have more conditions to check, you can add an else if statement. Think of it like an extension- it gives you more options to choose from based on what's being checked.
This image shows you an example of an if...else if...else statement.
Since we are on the topic of having multiple conditions, let's try using a switch statement. A switch statement is useful when you want to execute different code blocks based on different values of the condition.
Let's look at another example!
What do you think will print to the console?
It will print "red" and "Roses are red" to the console. Since the value of the variable matches a case in the switch statement, it can execute the code block associated with that case. Just in case, here's a picture of how a switch statement works.
Reminder: Don't forget your default statement!
Notice how each case has a break statement? When the code finds the matching condition, it will stop the code right there and not run the remaining cases. This prevents JavaScript from executing unnecessary code, which can help save execution time.
Do you understand conditionals now? Well, I suppose that question is only for those who needed a bit of guidance. Just remember: conditionals are a lot like story-based games. Once you pick a path, you can't turn back--you have to see the outcome of your decision.
THINGS TO NOTE
There are some things you should keep in mind when working with conditionals. Of course, you can learn along the way, but I thought I would just give you a few key points to note.
1. Be Mindful of which equality sign you use.
In JavaScript, there are two equality operators. The strict equality operator '===' and loose equality operator'=='. It's highly recommended to use the strict equality, as it can help you avoid bugs that are caused with type coercion. Type coercion is when JavaScript automatically converts values to match types when comparing things to each other.
Here is an example of type coercion.
However, this would give us false because 'age' is a number and 'basketballNumber' is a string.
2. Make things simple.
Don't overcomplicate your conditions unless it's necessary. Keeping them clean and easy to understand makes your code more readable.
3. Always, always test your code
Make sure you test all possible outcomes of your code. Whether the condition evaluates to true or false, your code should run smoothly and behave as expected in every scenario.
That's it for now ! Thanks for reading my first blog, and happy coding!
Top comments (0)