- These source code examples are up on Github
1.1 - First basic if statement example
1.2 - Not using brackets
If statements can have brackets or not, I generally always do use brackets with if statements even if it does have only one line of code because I find that it helps to make things more clear to me. There are a number of other reasons why to avoid doing this that might prove to be more important than just person preference. However this is just one of many things that one might come across when reading code on the open web, and more often than not it might not present a note worthy problem.
1.3 - Using else after a starting if statement
The else keyword can be used right after an if statement block. This is a good place to park some code that I will want to run if, and only if the expression evaluates to false. So then the use of else differs from just placing some code after an if statement as that code will always run regardless of the boolean state of the if statement, while code in an else block will only run in the event of a false condition of the first opening if statement.
1.4 - The ternary operator
There are if statements, but then there is also the question of an expressions friendly form if an if statement. When it comes to that there is the ternary operator.
1.5 - Using a switch
Another control flow option aside from if statements and the ternary operator would be a switch statement.
1.6 - Object keys, functions, and if statements
I can not say that I use a switch often, in fact when I think about it I never use them. I am not necessary going to get on to some kind of anti switch war path about it mind you, it is just that there are other options that I seem to prefer when it comes to getting into more advanced control flow topics. A switch works by evaluating an expression as a value, and then comparing that value to one or more cases and if one is found that is a match that code will run, if not do code will run or a default case will run. That is all fine and good but why not have an object with key values, and use the in operator with an if statement in the body of a function where I can use the return keyword? In the event that a value is a key of the object return the corresponding key value, and if nothing is there just have some kind of default value for an additional return statement at the bottom of the function.
2 - Functions for the use of control flow
Another major part of control flow beyond just that of an if statement would be the use of one or more functions. There is a whole lot of ground to cover when it comes to functions, so I am not going to be doing them justice in this section for sure that would be a major understatement. However staying on topic with the idea of control flow, it might be called for to have at least a few examples involving functions.
2.1 - Else is not needed when making a function that returns something
If a function that is being made that is using the return keyword to return a result when called then else does not need to be used. The reason why is that return will stop any further execution of any additional code, so it can be used as a way to break out of a function. This differs from blocks of code where I might only want some code to run if and only if a condition is not met.
2.2 - Object keys revisited with and IIFE
In the basic section of this post I introduced a simple example of using a function along with an object as an alternative way of doing more or less the same thing as with a switch statement. In this example I am now taking a second look at this sort of thing and doing just a bit more with it such as enclosing the object as well as the function inside the body of another self executing function called an IIFE. This results in a kind of module design where everything is wrapped up into one nice need little package that can be used by way of a single global variable, also if I enclose something like this along with everything else I am using into the body of another IIFE then I will not be defining any globals at all.
3 - Conditional operator
Because the conditional operator is a kind of expression it can be used in conjunction with if statements.
So the conditional operator is helpful because I can use it in an expression, doing so with if statements is a little complicated often involving the use of an IIFE in the expression with results in hard to read code that is harder to debug.
4 - State machines as a way to control logic
Another way to control the flow of logic is to use what is often called a state machine. This is typically a collection of two or more methods that will fire only when a variable or object property is a given value that will cause the method to fire, or at least that might be what one is in a very basic form. Really getting into this subject can end up getting a little complex, and as such truly doing it justace would require writitng a whole other post on the topic alone, and maybe event a few such posts.
However the basic idea of a state machine is simple enough, with that said I could just have an object, and a bunch of methods for a bunch of key names of this object. I can then have a property that will be the key name of the current method to fire when a main tick or update method is called. I then have some kind of way to change the value of this property that will control what method to fire each time an update method is called.
In other words something like this maybe.
There are a number of other ways that a state machine could be made like this. There is also thinking more in terms of a collection of objects like this other than just one. So yest this subject can get a little advanced.
State machines come into play when working on some kind of project that is a little advanced in which there are many application states. Such as a state in which assets need to be loaded, and a state that will run when all those assets are done loading. I often work out this kind of system when making canvas games, even ones that do not involve such a task, because it is often just needs to break things down into many different states. For example there might be a main game state, but then there will be another state that will set up some settings before starting a new game in the main game state, and yet another state that is a main title menu, and so forth.
Speaking of canvas examples I have a main post on canvas examples that might be worth checking out, but when it comes to state machines alone I have a single canvas example where that is the focus. In addition I have many canvas examples where a state machine is very much a part of the over all project, one of my best examples thus far might be my pop the lock game that features a fairly complex state machine module.
5 - Array methods, and A word on when not to use if statements
5.1 - The Array.filter prototype method
Say I have an array of mixed values that contain numbers, and all kinds of other types. What I want is to have an array of just numbers from this source array. I could work out a function that will make use of a while loop, to loop over the contents of this source array. Then inside the body of the while loop I can use an if statement with an expression that make use of the typeof operator to check the type of a current element in the source array. In the event that an element is indeed a number I can then push that element to a new array, and then when done return this new array.
6 - Conclusion