Javascript if statement
Created By: chatGPT
JavaScript provides the You can also use the In addition to if and else, JavaScript allows the use of When writing conditions, you can use comparison operators like It’s also common to use logical operators like
if
statement as a basic control structure used to execute a block of code based on a specified condition. If the condition evaluates to true
, the code inside the if block will run. If it's false
, the code will be skipped.if (condition) {
// Code to execute if condition is true
}
else
statement to define a block of code that runs when the condition is false. This is useful for handling different scenarios based on the condition's evaluation.if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
else if
to evaluate multiple conditions sequentially. This enables you to test for additional criteria if the first condition fails.if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if none of the conditions are met
}
===
, !==
, <
, >
, <=
, and >=
to form logical expressions.let num = 10;
if (num > 5) {
console.log('Number is greater than 5');
} else {
console.log('Number is 5 or less');
}
&&
(and) and ||
(or) to create more complex conditions. This allows for multiple criteria to be evaluated in one statement.if (condition1 && condition2) {
// Code if both conditions are true
}
if (condition1 || condition2) {
// Code if at least one condition is true
}