Javascript If/Else :
JavaScript's if...else statement is a conditional statement that enables you to run various code
blocks in accordance with a given condition.
It uses the following basic syntax:
if (condition) { // If the condition is true, run this code block } else { // If the condition is false, run this code block }
How the if...else statement functions is as follows:
An evaluated boolean expression serves as the condition. The code block after the if statement is
run if the condition evaluates to true. assuming the condition evaluates to false, the code
block that comes after the else statement is run (assuming it is there).
One or more sentences are contained within curly brackets to form the code block that corresponds
to the if statement. If the requirement is met, these assertions will be carried out.
When the condition results in a false value, the else block (optional) is run that may contain one or
more statements.
Here's an example that shows the usage of if...else statement:
let num = 7; if (num % 2 === 0) { console.log("The number is even."); } else { console.log("The number is odd."); }
In this example, the condition num % 2 === 0 checks if num is divisible by 2 without a remainder.
If the condition is true, it prints "The number is even." Otherwise, it prints "The number is
odd."
You can also use multiple if...else statements in succession to handle multiple conditions:
let age = 18; if (age < 13) { console.log("Child"); } else if (age >= 13 && age < 18) { console.log("Teenager"); } else { console.log("Adult"); }
In this example, different age ranges are checked, and the appropriate message is printed based
on the condition.
The if...else statement allows you to control the flow of your program based on specific
conditions, making your code more dynamic and adaptable to different scenarios.
Comparison operators in conditional statements :
Comparison operators are commonly used in conditional statements, such as if statements, to evaluate conditions and make decisions based on the comparison result. JavaScript provides several comparison operators to compare values. Here are the commonly used comparison operators in conditional statements:
// with equality operator let num = 5; if (num == 5) { // Code block executed if num is equal to 5 } // with strict equality operator let num = 5; if (num === 5) { // Code block executed if num is equal to 5 and has the same type }
The equality operator == compares two values for equality, performing type coercion if necessary.
While The strict equality operator === compares two values for equality without performing type
coercion. It checks both the value and the type.
We can use other operators also in IF condition like (!= or !==), (<, >, <=, >=) etc.
Short-circuit evaluation :
Short-circuit evaluation is a behavior exhibited by logical operators (&& and ||) in JavaScript. It refers to the process of evaluating the operands of these operators from left to right and stopping as soon as the result can be determined without evaluating the remaining operands.
// Using short-circuit evaluation to avoid potential errors let name = "John"; if (name && name.length > 0) { console.log("Name is present and not empty"); } // Using short-circuit evaluation for default values function greetUser(name) { name = name || "Guest"; console.log("Hello, " + name); } greetUser(); // Output: Hello, Guest greetUser("Alice"); // Output: Hello, Alice
In the first example, the name variable is checked for truthiness and then further evaluated for
its length only if it is truthy. This prevents an error when name is null, undefined, or an empty
string.
In the second example, the name parameter of the greetUser function is assigned a default value
of "Guest" using short-circuit evaluation. If name is a falsy value (such as null or undefined),
the right operand is evaluated, and the default value is assigned.
By leveraging short-circuit evaluation, you can write more concise and efficient code by avoiding
unnecessary evaluations and handling default values in a straightforward manner.