In this tutorial we will show you the solution of how to write an if statement in JavaScript, as general we know that there are many types of control structures in JavaScript like if statement, if else statement and switch statement etc.
The first and basic control structure is if statement. So, today we learn about how to use and write if statement in JavaScript.
Step By Step Guide On How To Write An If Statement In JavaScript :-
These types of statement is also known as conditional statement because these are used to apply conditions.
Like in if statement, it executes the block of statement if the condition of if statement is true.
Otherwise if conditions is false, then nothing will be executed that is inside if statement and the flow of code continues to code written after if statement block. Let us see the syntax of if statement first.
if (condition) { // block of code to be executed if the condition is true }
Here, you see that if statement first checks for the condition.
If the condition become true then the code inside if statement is executed otherwise if the condition is false then code inside if statement is skipped.
Now, we will show you an example related to if statement. So, that we are able to understand if statement clearly.
<!DOCTYPE html> <html> <head> <title> JavaScript tutorials </title> </head> <body> <h1> JavaScript Tutorials by TalkersCode </h1> <h2> how to write an if statement in JavaScript </h2> <script> var example = 50; if( example > 30){ document.write("Value of example is greater than 30 "); } </script> </body> </html>
- Here, above we show you an example in JavaScript.
- For this first of all we here create a basic structure of html. For this we use html, head, title and body with script tag. We hope you know about the basic structure of html and which html tag is used for which concept.
- Now, when we look inside body tag. Here, we see some headings tag and a script tag. Heading tag again is a concept of html, whereas script relates to JavaScript.
- JavaScript is written always inside script tag. Where script is a paired tag so it also has a closing tag. Now inside these script tag, we write our JavaScript code.
- Now, here we first create a variable with name example using var and then assigns a value to it that is 50.
- After this the main concept arises of if statement. As we see here we use our if statement. For this we write our syntax of if statement. For giving a condition to if we use example > 30.
- It means that the block of if statement is executed only if the value of example is greater that 30. As we know that our value of example is 50. So, the block of if statement is executed and line with text Value of example is greater than 30 shows on display. We hope you understand the above example easily.
Conclusion :-
At last in conclusion, here we can say that with the help of this article we are able to understand how to write an if statement in JavaScript.
I hope this tutorial on how to write an if statement in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.