Skip to main content

JavaScript Conditional Statements

 

JavaScript Conditional Statements with Code Examples by Ziggy Rafiq

JavaScript's Conditional Statements

Overview 

JavaScript's conditional statements provide the ability to execute different blocks of code depending on whether a certain condition evaluates to true or false. In this way, developers can create dynamic code that reacts to changing data or user input. Here are some examples of how to use conditional statements in JavaScript.

If Statement

The if statement is the most basic conditional statement in JavaScript. It allows you to execute a block of code only if a certain condition is true.

Example One

Ziggy Rafiq If Statement Code Example One

In the above example, I have given the code checks whether the age variable is greater than or equal to 18. If it is, the first block of code (inside the curly braces) will be executed and "You are old enough to vote." will be logged to the console. If the condition is false, the second block of code will be executed and "You are not old enough to vote yet." will be logged into the console.


Example Two

Ziggy Rafiq If Statement Code Example Two

In the above example, I have given a very basic if statement, which executes the block of code only if the number is above zero.

Else-if Statement

The else-if statement allows you to add more conditions to your code. It will only be executed if the previous condition is false.

Example One

Ziggy Rafiq Else If Statement Code Example One

In the above example, I have given a very basic if and else statement, which executes the block of code if the number is above zero as positive and as I have set the number to -3 it will execute the else statement.


Example Two

Ziggy Rafiq Else If Statement Code Example Two

In the above example, I have given the code that checks the value of the grade variable and executes the corresponding block of code based on its value. If the grade is greater than or equal to 90, "You got an A!" will be logged into the console. If the grade is between 80 and 89, "You got a B!" will be logged into the console. If the grade is between 70 and 79, "You got a C!" will be logged to the console. If none of the conditions is true, "You failed the class." will be logged to the console.


Switch Statement

The switch statement is another way to add multiple conditions to your code. It's useful when you have a lot of conditions to check and you don't want to use a lot of else-if statements.

Example One

Ziggy Rafiq Switch Statement Code Example One

In the above example, I have given the code that checks the value of the day variable and executes the corresponding block of code based on its value. If the day is "Monday", "It's Monday." will be logged to the console. If the day is "Tuesday", "It's Tuesday." will be logged to the console, and so on. If none of the conditions is true, "It's the weekend!" will be logged to the console.

Example Two

Ziggy Rafiq Switch Statement Code Example Two

In the above example, I have given the switch statement to check the value of the fruit variable against three possible values. If the value is "banana", "This is a banana." is printed. If the value is "orange", "This is an orange." is printed. If the value is "apple", "This is an apple." is printed. If none of these values matches, the default block of code is executed and "I don't know what fruit this is." is printed.


Final Thoughts 

These are just a few examples of how you can use conditional statements in JavaScript. There are many more ways to use them, depending on your specific use case. As conditional statements in JavaScript provide a powerful way to create flexible and reactive code. By using if and switch statements, developers can make their code respond to changing data or user input in dynamic and intelligent ways.



Comments

Most Viewed Ziggy Rafiq Blog Posts

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6 Overview An Enum (Enumeration) is a group of constants that are read-only value types. By default, the first value of the Enum variable is 0 i.e. Here we will create an Enum of Priorities type with read-only values of Highest, Normal and Low. We will set the read-only values using an integer assigning a number next to the value. By default, the integer value will start with 0. Here we will be assigning the integer value next to the Enum value such as in the below example and we will use a comma (,) to separate the item in the list of Enum(Enumeration).  We create Enum by using the Enum keyword and then using class, interface, and abstract. The reason we use an Enum is to ensure we improve our application performance and improve application readability, and maintainability, and reduces the complexity of the application hence why if you take a look at the example below of Status (NotStarted, Started, Complete

A Complete Guide to Using GUIDs in C# with Code Examples

  Overview In this post, we are looking at GUIDs (Globally Unique Identifiers), which are widely used in C# for generating unique identifiers for objects, entities, and resources in a system. In this post, we'll explore the basics of GUIDs in C#, their advantages, and how to convert strings to GUIDs. In this post, we have used Guid Generator to create the GUID from the following URL Address https://guidgenerator.com/ What is GUID GUID (Globally Unique Identifier) in C# is a 128-bit value that is used to identify objects, entities, or resources in a unique manner across different systems and applications. It is also known as UUID (Universally Unique Identifier) in some other programming languages.   GUIDs are generated using a combination of unique factors such as the MAC address of the network adapter, the current time and date, and a random number. The resulting GUID is a string of 32 hexadecimal digits separated by hyphens, such as "b86f2096-237a-4059-8329-1bbcea72769b&

Primitives Data Types and None-Primitives Data Types in C# with Code Examples

  Overview I wrote this post to provide an explanation of primitive and non-primitive data types in C#. C# is a strongly typed programming language, where each variable and expression must have a specific data type. C# data types are categorized into two primary groups: primitive data types and non-primitive data types. Primitive data types are the simplest data types available in programming languages. They are typically pre-defined data types and can represent a single value, such as a boolean value, character, or integer. Examples of primitive data types include int, char, float, double, and boolean, which are common in programming languages like C++, C, and Java. Non-primitive data types are also referred to as composite data types or reference data types. They are constructed from primitive data types and are more complex than primitive data types. Non-primitive data types can hold multiple values and allow for the creation of more intricate data structures like tables, lists,