DEV Community

Kuhanraja A R
Kuhanraja A R

Posted on

javascript conditional statements

Conditional statements:

JavaScript conditional statements allow you to execute specific blocks of code based on conditions. If the condition is met, a particular block of code will run; otherwise, another block of code will execute based on the condition.

Let see if statement:

The if statement:

The if statement evaluates a condition inside parentheses. If the condition is true, the block of code inside the curly braces {} runs. If it’s false, it skips that block.

let mark = 36;
let result;
if(mark >= 35)
{
result = "pass"
}
else{
result = "fail"
}
Enter fullscreen mode Exit fullscreen mode

The if else statement:

The if-else statement will perform some action for a specific condition. Here we are using the else statement in which the else statement is written after the if statement and it has no condition in their code block.

if (ind>pak){
   console.log("ind win")
}else if ( pak > ind){
   console.log("pak won")
}  else{
console.log("draw")
}
Enter fullscreen mode Exit fullscreen mode

Nested if else statement:

A nested if...else statement is an if...else structure placed inside another if or else block. This allows you to check multiple conditions in a hierarchical or layered way, making complex decision trees possible.

if (budget <= 25000)
{
  if (budget == "samsung")
    {
      if(cam == "64mp")
        else{
              (cam == "48mp")
            }
     }  
else if{
      if (brand == "oppo") {
      if (cam == "72mp") {
      }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
alexmustiere profile image
Alex Mustiere

And what about the switch?

Collapse
 
little_coding_things profile image
Little Coding Things Blog

Yeah as @alexmustiere said, probably best to include a switch or even a if/switch combo, that would be nice for anyone checking this post.

That said, I personally wouldn’t follow the pattern shown in the third example for production code. But I guess that’s subjective and depends on the codebase’s best practices and guidelines.