Learn HTML,CSS, Python etc from the foundation

Sunday, February 9, 2020

JAVASCRIPT FREE COURSE-Lesson 10

No comments :

Hi guys, I will be discussing with you in this lesson a very important topic in JavaScript. The topic is "if Statement". JavaScript is a sequential programming language and it follows instructions and obeys certain conditions.   "if statement" is a conditional statement in JavaScript. 

Javascript if statementWhenever a programmer or a web developer writes codes in JavaScript, he or she usually wants to carry out different actions 
using conditional statements such as The if Statement, else state, if…else statement, etc.

In this lesson, I will place more premium on teaching you about The if Statement. In our subsequent lessons, you will, however, learn other conditional statements. This is a “one-step-at-a-time” technique I employed in order to ensure you understand every bit of my lessons without any iota of complexity.

The Syntax of the if Statement
if (condition) {
   statements
}

EXPLANATION

 1.     The word “condition” in the brackets in the first line is the place you stipulate the conditions that must be followed in order for the code to be executed. 
 2.     The word “statements”  between the two curly brackets is where you state or stipulate what will be executed only if the specified condition in the first line is true.

Now open your text editor and punch the following scripts into the body section
<script>
var firstNum1 = 25;
var secondNum2 = 60;
if (firstNum1 < secondNum2) {
    alert("I am learning JavaScript with codinglegit free of charge.");
}
</script>

Save it and then run the code to see the amazing result!

NOTE THIS!

1.     As you can seen in the image above, the JavaScript alert() method is used to generate a popup alert box that contains the information provided in parentheses.
2.     Note also that the word  “if” is in lowercase letters. Uppercase letters of this word (that is, “If” . “iF” or “IF”) will automatically generate an error.

WRAPPING UP

Whenever you want to a block of code to be executed  if a specified condition is true, then you should use the if statement


No comments :

Post a Comment