cover-img

What is Condition in JavaScript?

20 January, 2023

10

10

2

What is Condition in JavaScript?

In JavaScript, a condition is an expression that evaluates to a boolean value, either true or false. You can use conditions to control the flow of your program, by executing different code blocks based on whether a condition evaluates to true or false.

Here's an example of how you can use a condition in JavaScript:

const x = 10;

if (x > 5) {
console.log("x is greater than 5");
} else {
console.log("x is not greater than 5");
}

In this example,

  • The if statement checks whether the value of x is greater than 5.
  • If it is, the code block following the if statement is executed, and the message "x is greater than 5" is printed to the console.
  • If x is not greater than 5, the code block following the else statement is executed, and the message "x is not greater than 5" is printed to the console.

You can also use multiple conditions by using else if statements. For example:

const x = 10;

if (x > 15) {
console.log("x is greater than 15");
} else if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is not greater than 10 or 15");
}

In this example,

  • The first condition checks whether x is greater than 15.
  • If it is, the code block following the first if statement is executed.
  • If it is not, the second condition is checked, and if x is greater than 10, the code block following the second if statement is executed.
  • If neither of these conditions are true, the code block following the else statement is executed.

You can also use a ternary operator, which is a shorthand way of writing a simple if statement. The syntax for a ternary operator is:

condition ? value1 : value2
  • If the condition evaluates to true, the expression returns value1,
  • Otherwise it returns value2.

Here's an example of how you can use a ternary operator:

const x = 10;
const y = x > 5 ? "x is greater than 5" : "x is not greater than 5";

console.log(y); // prints "x is greater than 5"

In this example, the ternary operator checks whether x is greater than 5. If it is, the string "x is greater than 5" is assigned to the yvariable, and if it is not, the string "x is not greater than 5" is assigned to the y variable.

The Switch-Case

In JavaScript, the switch statement is used to execute different code blocks based on the value of an expression. The syntax for a switch statement is:

switch (expression) {
case value1:
// code to execute if expression == value1
break;
case value2:
// code to execute if expression == value2
break;
...
default:
// code to execute if expression does not match any of the above values
}

Here's an example of how you can use a switch statement in JavaScript:

const x = "apple";

switch (x) {
case "apple":
console.log("x is an apple");
break;
case "banana":
console.log("x is a banana");
break;
default:
console.log("x is not an apple or a banana");
}

In this example,

  • The switch statement evaluates the value of x, and then executes the code block corresponding to the first case that matches the value of x. In this case, x is "apple", so the code block following the case "apple" statement is executed, and the message "x is an apple" is printed to the console.
  • It's important to include a break statement at the end of each case code block, to ensure that execution does not continue into the next case. If you omit the break statement, the code for all subsequent case statements will be executed, until a break statement is encountered.
  • If the value of the expression does not match any of the case values, the code block following the default statement is executed. The default statement is optional, and can be used to specify a code block to execute if none of the case values match the expression.

You can also use a switch statement to execute code based on the type of an expression, by using the typeof operator. For example:

const x = 10;

switch (typeof x) {
case "number":
console.log("x is a number");
break;
case "string":
console.log("x is a string");
break;
default:
console.log("x is not a number or a string");
}

In this example, the switch statement evaluates the type of x, and then executes the code block corresponding to the first case that matches the type of x. In this case, x
is a number, so the code block following the case "number" statement is executed, and the message "x is a number" is printed to the console.

10

10

2

ShowwcaseHQ
Showwcase is where developers hang out and find new opportunities together as a community

More Articles