DEV Community

Vinayagam
Vinayagam

Posted on

JavaScript Conditional Statements: Ternary, Truthy/Falsy, and Switch Explained

1.Ternary Operator
definition:
The ternary operator is a conditional operator that takes three operands (three parts). It is a short and simple way to write an if-else statement in a single line.

It is called “ternary” because:

  • The word ternary means three.
  • It works using three expressions.

Syntax (JavaScript):
condition ? expression_if_true : expression_if_false;

  • If the condition is true, the first expression executes.
  • If the condition is false, the second expression executes.

Questions and Answers
1.What is the purpose of the ternary operator?
The ternary operator is used to make decisions in a short and simple way. It replaces small if-else statements and makes the code more concise.

2.Write a program to check whether a number is even or odd using

ternary operator.
let number = 10;
let result = number % 2 === 0 ? "Even" : "Odd";
console.log(result);
Enter fullscreen mode Exit fullscreen mode

3.Can the ternary operator return a value?
Yes. The ternary operator always returns a value.

Example:

let marks = 75;
let result = marks >= 40 ? "Pass" : "Fail";
Enter fullscreen mode Exit fullscreen mode

4.Can ternary operator replace all if-else statements?
No. It is suitable only for simple conditions.
For complex conditions, loops, or multiple statements, if-else is better.

5.When should we avoid using ternary operator?
Avoid it when:

  • Logic is complex
  • Multiple statements are needed
  • Code becomes hard to read

2.Truthy and Falsy in JavaScript

Definition:
In JavaScript, every value has an inherent Boolean meaning when evaluated in a condition (like in if, while, or ternary operator).

  • A truthy value is a value that is considered true when converted to a Boolean.
  • A falsy value is a value that is considered false when converted to a Boolean.

JavaScript automatically converts values to true or false in conditional statements. This is called type coercion.
**
Questions and Answers**
1.What are truthy values in JavaScript?
Examples:

true
1
"Hello"
[]
{}
-10
Enter fullscreen mode Exit fullscreen mode

2.What are falsy values in JavaScript?
Falsy values are values that evaluate to false in a Boolean context.
Examples:

false
0
""
null
undefined
NaN
Enter fullscreen mode Exit fullscreen mode

3.How does JavaScript check truthy or falsy values?
JavaScript automatically converts values to Boolean when used inside:


if statements

while loops

Ternary operators

Logical operators (&&, ||, !)
Enter fullscreen mode Exit fullscreen mode

3.Switch Statement in JavaScript
Definition:

In JavaScript, a switch statement is used to execute one block of code among many options based on a specific condition.

It is mainly used when you have multiple possible values for one variable and want to compare them clearly instead of writing many if-else statements.

syntax:

switch(expression) {
  case value1:
    // code block
    break;

  case value2:
    // code block
    break;

  default:
    // default code block
}

Enter fullscreen mode Exit fullscreen mode

1.What is the purpose of a switch statement?
The switch statement is used to select one block of code from multiple options based on a variable’s value.

2.Why is break important in switch?
The break statement stops execution after a matching case.
Without break, execution continues to the next case. This is called fall-through.
Example without break:

let num = 1;

switch(num) {
  case 1:
    console.log("One");
  case 2:
    console.log("Two");
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)