JavaScript Basics-Conditional Statements?: Part 3
By: Charles Tillmon

JavaScript Basics-Conditional Statements: Part 3

Sometimes in programming languages the code needs to make a decision on what actions to take dependent upon the conditions it is presented with. JavaScript is not outside of the scope of this concept. So in order to make these decisions conditional statements are embedded within the code. For example if you have a weather app and want to know the temperature outside of your house, how is this achieved? What purpose would the app serve if it showed you the temperatures of locations that had no association to you? This is why conditionals are important in the world of coding.

In the actual JavaScript code all of the possibilities are written into the script. However the main purpose of applications is to serve the end user. End users needs vary and that means the content delivered to the different entities must as well. Conditional statements allow the computer to make a decision based on inputs and produce what it believes is the best possible output for the client. This is the foundation that makes web applications possible.

JavaScript carries 4 conditional statements consisting of (if, else, else if, and switch). When using an if statement you must present it with a condition. If this condition proves to be true then the code block associated with the if statement will execute. If the condition proves false then the code block will be ignored.

No alt text provided for this image

The else statement adds to the if statement in the case the initial condition proves to be false and you need alternative code to run. Once proved false the computer will ignore the code followed by the if statement and immediately run the code block following the else statement.

No alt text provided for this image

The else if statement would be sandwiched between the if and else conditional statements. In the case that the first conditional proves false, the else if statement would provide a second conditional to test. If proven true the code following the else if statement would run, but if false the computer would ignore the provided code and run the code block following the else statement.

No alt text provided for this image

The last of these conditional statements would be the switch statement. This statement is given a condition or expression that once evaluated is compared to multiple cases that are contained within the code block. If the proper case is found that particular code block would be executed. If no match is found the default code would be executed to break the statement.

No alt text provided for this image

JavaScript also provides a ternary operator that can be utilized for smaller conditional statements. This performs the same function as an if statement but is condensed as a one line expression.

No alt text provided for this image

It is also important to mention the logical and comparison operators when speaking of conditional statements. Comparison operators are used to determine the equality or difference between two variables or values. Logic operators are used to determine the logic between two variables or objects. These help construct more complex conditions to represent the variety of possible situations a computer may have to make a decision on.


Additional Resources


要查看或添加评论,请登录

社区洞察

其他会员也浏览了