DEV Community

Cover image for If_Else Statements: The Quick Guide For Beginners
Deborah Kurz
Deborah Kurz

Posted on

If_Else Statements: The Quick Guide For Beginners

(See The Header Image For How An If_Else Statement Is Basically Written In A Function)

JavaScript likes to "solve" If_Else Statements like a simple "do thing 1 OR do thing 2" scenario:

Our function starts out by checking the condition with the block of code to the right of the "if". If the condition is true (it matches exactly with the thing we've defined to the right of the ===), the function will return something from this block of code, then exit the function.

BUT, if the condition is FALSE, (it DOESN'T match exactly with the thing we've defined to the right of the ===), the function will move on and run the ELSE part of the code.

We're essentially saying: Check the "if" part of the code, and if it matches, run the "if" part of the code. But if it doesn't match, run the "else" part of the code.

You can remember when to use If Statements like this:

If Thing 1 is TRUE, do Thing 1. If Thing 1 is FALSE, do Thing 2.

Top comments (0)