Learn about JavaScript IF STATEMENTS

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

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.