![]() |
VOOZH | about |
In programming, there will be many occasions in which you will want different blocks of code to run depending on user input or other factors.
As an example, you might want a form to submit if each field is filled out properly, but you might want to prevent that form from submitting if some required fields are missing. In order to achieve tasks like these we have conditional statements, which are an integral part of all programming languages.
Conditional statements execute a specific action based on the results of an outcome of true or false.
A few examples of JavaScript conditional statements you might see include:
Conditional statements are part of the logic, decision making, or flow control of a computer program. You can compare a conditional statement to a βChoose Your Own Adventureβ book, or a flowchart.
In this tutorial, we will go over conditional statements, including the if, else, and else if keywords. We will also cover the ternary operator.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
JavaScript is a high-level, object-based, dynamic scripting language popular as a tool for making webpages interactive.
Browse Series: 37 tutorials
Software engineer and open source creator
Community and Developer Education expert. Former Senior Manager, Community at DigitalOcean. Focused on topics including Ubuntu 22.04, Ubuntu 20.04, Python, Django, and more.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
In the example with the grades.js is maybe a better example to use the switch instead of the if () else if construction. And a nice addition to the article could be explaining the triple = with the type checking. β0β === 0 will fail but β0β == 0 will work. https://codepen.io/stefferd/pen/OjqaOM
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.