Learn about JavaScript IF STATEMENTS: If statements are a way of telling our browser to perform an instruction only if the condition of our variable is true. If the condition of our variable is false, our browser won’t perform the instruction in the code.

We have to structure if statements in a certain way. We start them with the statement keyword (if). Then we open a pair of brackets and put the rule for our if statement inside them. Then we open a pair of curly braces ({ }). Inside them, we put the instruction we want to happen only when our if statement is true.

Let’s have a look at an example where we’ve used the equal to operator (==) to create an if statement. Here the value of our variable is set to Angelika using the assignment operator (=). We then create an if statement by asking our browser to check if the value in our variable is equal to (==) Angelika. Then we ask our browser to pop up an alert if our condition is true. Here is sample code:

<script>
 var person = "Angelika";
 if(person == "Angelika") {
 alert("Hello Angelika, remember! one step at a time! baby steps.");
 }
</script>

Related Links:

Online JavaScript Compiler. Code on the go.
JavaScript Glossary – if statements
Else..if – JavaScript – MDN