types of conditional statements in javascript
Introduction: JavaScript, being a versatile and widely used programming language, offers several ways to control the flow of your code. Conditional statements are fundamental constructs in programming that allow you to execute different blocks of code based on certain conditions. In JavaScript, these conditional statements are crucial for creating dynamic and interactive web applications. In this article, we’ll delve into the various types of conditional statements in JavaScript along with practical examples to illustrate their usage.
1) if Statement:
The if statement is one of the most basic conditional statements in JavaScript. It allows you to execute a block of code if a specified condition evaluates to true.
Syntax-
if (condition)
{
Statement // true block
}
Example-1
<html>
<script>
var x=prompt(“Enter any number”)
if (x>20)
{
alert(x+ ” is greater than 20″);
}
</script>
</html>
Output
If we enter x value is 25 then you will get the following output
25 is greater than 20
2) if…else Statement:
The if…else statement extends the functionality of the if statement by providing an alternative block of code to execute when the condition is false.
Syntax-
if (condition)
{
Statement // true block
}
else
{
Statement // false block
}
Example-1
<html>
<script>
var x=prompt(“Enter any number”)
if (x>20)
{
alert(x+ ” is greater than 20″);
}
else
{
alert(x+ ” is less than 20″);
}
</script>
</html>
Output
Enter any number-14
14 is less than 20
Example-2
<html>
<script>
var a=parseInt(prompt(“enter the value of a”))
var b=parseInt(prompt(“enter the value of b”))
if (a>b)
{
document.write(a+” is greater than “+b)
}
else
{
document.write(b+” is greater than “+a)
}
</script>
</html>
Output
First Run
Enter the value of a 40
Enter the value of b 25
40 is greater than 25
Second Run
Enter the value of a 60
Enter the value of b 85
85 is greater than 60
Example-3
<html>
<script>
var n=parseInt(prompt(“enter any number”))
if (n%2==0)
{
document.write(n+” is even”)
}
else
{
document.write(n+” is odd”)
}
</script>
</html>
Output
First Run-
Enter any number 24
24 is even
Second Run
Enter any number 13
13 is odd
3) if…else if…else Statement:
This statement allows you to evaluate multiple conditions and execute different blocks of code accordingly.
if (condition 1)
{
Statement // true block
}
else if(condition 2)
{
Statement // true block
}
else
{
Statement //code to be execute if neither condition 1 nor condition 2 is true
}
Example-1
<html>
<script>
var n=parseInt(prompt(“enter any number…”))
if (n>20)
{
document.write(n+ ” is greater than 20″)
}
else if(n<20)
{
document.write(n+ ” is less than 20″)
}
else if(n==20)
{
document.write(n+ ” is equal to 20″)
}
else
{
document.write(“Invalid Input”)
}
</script>
</html>
Output
First Run-
Enter any number
14
14 is less than 20
Second Run
Enter any number
20
20 is equal to 20
For Live Class on youtube click on the below link
Example-2
<html>
<script>
var a=parseInt(prompt(“enter first number…”))
var b=parseInt(prompt(“enter second number…”))
var c=parseInt(prompt(“enter third number…”))
if (a>b && a>c)
{
document.write(a+ ” is greater than “+b+” and “+c)
}
else if(b>a && b>c)
{
document.write(b+ ” is greater than “+a+” and ” +c)
}
else
{
document.write(c+” greater than “+b+” and “+a)
}
</script>
</html>
Output
enter first number… 50
enter second number…80
enter third number…10
80 is greater than 50 and 10
For Full JavaScript Classes Subscribe Our YouTube Channel link is given below
https://www.youtube.com/@olevelguruji
4) switch Statement
The switch statement provides an alternative way to handle multiple conditions more efficiently than chaining if…else if statements.
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
…………………
………………..
default:
code to be executes if it is different from case 1 and case 2
}
Example-1
<html>
<script>
var ch=parseInt(prompt(“enter your choice…”))
switch(ch)
{
case 1:
document.write(“Monday”);
break;
case 2:
document.write(“Tuesday”);
break;
case 3:
document.write(“Wednesday”);
break;
case 4:
document.write(“Thursday”);
break;
case 5:
document.write(“Friday”);
break;
case 6:
document.write(“Saturday”);
break;
case 7:
document.write(“Sunday”);
break;
default:
document.write(“invalid Choice….”)
}
</script>
</html>
Output
Enter your choice 3
Wednesday
Enter Your choice 9
Invalid Choice
5) Ternary Operator (Conditional Operator):
The ternary operator (condition ? expression1 : expression2) is a concise way to write conditional statements. It evaluates a condition and returns one of two expressions based on whether the condition is true or false.
Example
<html>
<script>
var marks = prompt(‘Enter your marks :’);
// check the condition
var result = (marks >= 40) ? ‘pass’ : ‘fail’;
alert(result);
</script>
</html>
Conclusion: Conditional statements are indispensable tools in JavaScript programming for implementing logic and making decisions based on different conditions. Understanding how to use if, if…else, if…else if…else, switch, and the ternary operator enables you to write more efficient and expressive code. By mastering these conditional statements, you’ll have greater control over the flow of your JavaScript programs, paving the way for building dynamic and interactive web applications.
Also Check Our Latest Upload
JavaScript operators with example
JavaScript programs examples with output
1 thought on “types of conditional statements in javascript”