Introduction π
Conditional statements are a fundamental part of JavaScript, allowing you to perform different actions based on different conditions. These statements control the flow of the code based on boolean conditions - a section of code will only run if the condition is true. Letβs explore the syntax and usage of various conditional statements in JavaScript: if
, if/else
, else if
, and switch
.
1. if Statement π
The if statement is one of the most basic and frequently used conditional statements in JavaScript. It is used to execute a block of code only if a specified condition is true. The if statement evaluates the condition inside its parentheses, and if the condition is truthy, the code block within the curly braces {} is executed.
|
|
Here are examples using if conditions with the operators ===
, !==
, and >
in JavaScript. Each example demonstrates a different aspect of comparison and logical evaluation.
1. Using === (Strict Equality Operator) π
The ===
operator checks for both value and type equality. It’s a strict comparison and does not perform type coercion.
|
|
This code will execute the console.log statement because favoriteNumber is exactly equal to 7 in both value and type.
2. Using !== (Strict Inequality Operator) β
The !==
operator checks whether two values are not equal in both value and type.
|
|
Here, the code will execute because age is not equal to 30 in value (even though they are of the same type).
3. Using >
(Greater Than Operator) β¬οΈ
The >
operator checks if the value on the left is greater than the value on the right.
|
|
This code will log the message since speed is greater than 30.
Truthy and Falsy Values in JavaScript β
Falsy Values π΄
In JavaScript, values can be either “truthy” or “falsy”. The falsy values are specific and include 0
, ""
(empty string), null
, undefined
, NaN
, and false
. Any other value in JavaScript is considered truthy.
Let’s look at examples with each falsy value:
|
|
None of these code blocks will execute because each condition is a falsy value.
Truthy Values π’
Anything that is not in the list of falsy values is truthy. This includes "non-empty"
strings, any number other than 0, objects
, arrays
, functions
, and the boolean
true.
|
|
In each of these examples, the condition inside the if statement is truthy, so the corresponding code block will execute.
2. if-else Statement βοΈ
The if/else statement in JavaScript allows you to execute one block of code if a condition is true and another block if the condition is false. It’s an extension of the if statement and is used for binary decision making - do one thing under one condition and something else under an alternative condition.
|
|
In this example, if temperature is greater than 25, the message “It’s warm outside!” will be logged to the console. Otherwise, the message “It’s cool outside!” will be shown.
Shortcut: Ternary Operator π
The ternary operator provides a shorthand way of writing an if/else statement. It takes three operands: a condition, followed by a question mark (?), then an expression to execute if the condition is truthy, followed by a colon (:), and finally the expression to execute if the condition is falsy.
Syntax of Ternary Operator:
|
|
|
|
This code assigns “Pass” to result if score is 50 or higher, and “Fail” if it’s lower than 50.
3. else if Statement π’
The else if statement is used for multiple conditions that are mutually exclusive.
|
|
4. Switch Statement π
The switch
statement is best used when you have multiple possible conditions and you need to execute different code for each condition.
|
|
Conclusion π
Understanding and correctly implementing conditional statements is vital in JavaScript programming. Each type (if
, if/else
, else if
, switch
) serves a specific purpose in controlling the flow of logic based on conditions. By mastering these concepts, you can write more efficient, readable, and dynamic JavaScript code, capable of handling a wide range of logical scenarios.