DEV Community

Santiago Arellano
Santiago Arellano

Posted on

if statements by a n00b

Alright everyone I consider myself a noob due that I have started coding not too long ago, here is my simple explanation for if statements.

So when someone tells us about an IF statement what do we think about? A condition right...? Just as we communicate this is the same thing.

If certain to scenario/condition happens then I will do this...

This is what it looks like in code:

if (condition) {
your code
}

Pretty simple right? So if the condition that we want is met, then it will execute the code your write in there, if NOT then it will just skip it. Let's look at something a bit more descriptive.

if (today === Friday) {
Party!
}

I bet you understood that one huh. Alright now, there are a bit more tricks to this like I'll show below.

ELSE:
This basically is a code of block that executes after the if statement IF the initial condition is not met.

if (today === Friday) {
Party!
} else {
How many more days til Friday?
}

ELSE IF:
This one would be like adding another condition to check after the first one is false. As we can see below, if today is not Friday or Thursday then we want to know how many more days until Friday.
The final else only executes if the 2 first conditions are not met.

if (today === Friday) {
Party!
} else if (today === Thursday) {
Ok, keep pushing, just one more day
} else {
How many more days til Friday?
}

I hope this helps out whoever needs it.

Thank you for reading.

Discussion (2)

Collapse
shane325 profile image
Shane Barry

Good job. FYI the syntax is else if, not if else

if (condition1) {
  //  block of code to be executed if condition1 is true
} else if (condition2) {
  //  block of code to be executed if the condition1 is false and condition2 is true
} else {
  //  block of code to be executed if the condition1 is false and condition2 is false
}
Enter fullscreen mode Exit fullscreen mode
Collapse
santiareh profile image
Santiago Arellano Author

thanks man I appreciate it, like I said n00b lol