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


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

Charles Tillmon的更多文章

  • CSS: Normal Document Flow

    CSS: Normal Document Flow

    As we focus more on understanding the styling layer of web pages it is only natural that we discuss the normal document…

  • Async & Await Keywords

    Async & Await Keywords

    When ES2017 was released along with it came the async and await keywords. These keywords were developed to make working…

  • JavaScript Promises

    JavaScript Promises

    Typically, with asynchronous code you will see the use of callback functions, which are functions that are passed as…

  • TCP/IP Overview

    TCP/IP Overview

    Have you ever wondered how data is transmitted between computers? Well just like us they must establish a common…

  • The Internet vs. The Web

    The Internet vs. The Web

    Have you ever used the Internet and the Web interchangeably? This is a common mistake that many have made, me included.…

  • Core Front-End Components Overview

    Core Front-End Components Overview

    Have you ever wondered how web pages get on the web? When it comes to basic web pages, typically they consist of 3 core…

  • Asynchronous vs. Synchronous Code

    Asynchronous vs. Synchronous Code

    JavaScript is a single threaded, synchronous programming language. This means when code is executed by the JavaScript…

  • Is Modernizing Your Business Worth The Investment?

    Is Modernizing Your Business Worth The Investment?

    “The day that you plant the seed is not the day that you eat the fruit”. This is a mantra that can apply to life, but…

  • JavaScript Basics-Functions: Part 4

    JavaScript Basics-Functions: Part 4

    JavaScript functions are blocks of code that either perform a task or return a value. These blocks of code are executed…

  • JavaScript Basics-Loops: Part 5

    JavaScript Basics-Loops: Part 5

    Loops in JavaScript are a way to repeat the same set of actions a set number of times or until a condition is met…

社区洞察

其他会员也浏览了