if…? Statements in JavaScript
An if statement is where there needs to be a conditional requirement, i.e. if this is true… we will do this instead of that. Not the best explanation but there will be a demonstration in code on how this works. So what we have is a boolean, something that is true or false. So with a boolean expression we want to have code where it asks: if this code is true, run this set of code, if this code is false, let’s doing something else.
So here in this example I have an argument of i setting it equal to zero, if it comes out to being correct, it will return a true boolean, if false it would do the opposite and return the boolean of false. Let’s say that I is set to three, now we have the question, is three equal to zero? The answer is no, and Im bad at math! Some will ask… why do we need this? Well, for certain code to run we need specific requirements to run certain lines of code, if we just ran the entire file without specific requirements there would be a ton of information that would be processed, which would slow down load times and in some worse cases, show passwords, users, IP even depending on what time of file it is. It’s best that we dont have those be shown publicly without have certain conditions met. It won’t always be that drastic but it really does help developers show certain information when the user calls upon it. Such as a button that reads “Show all Recipes” wont show all of the recipes without a requirement, can be an event listener (JavaScript) or be a CLI where you type in the recipes which will then show you all of the recipes that was asked for. TLDR is that if statements need a condition to be met for code to ran through, if that condition is not met, it could have a secondary option being an “else” statement which is shown in the sample above. So if the condition is not met the code will go through the else statement and run whatever code is within it. Thats pretty much the basics behind it!