JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print How to Print a Page Using JavaScript Textbox events in JavaScript How to find the largest number contained in a JavaScript Array?

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions

Javascript If Else

JavaScript use a conditional statement which is used to perform different tasks according to the conditions. There are three forms of statements use in JavaScript.

  1. if Statement.
  2. if else Statement.
  3. if else if Statement.

if statement

The if statement is used in JavaScript to execute the code if the condition is true or false.
if (condition){ //   
    //content to be evaluated
}
Example:-
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var x=50;  
if(x>30){  
document.write("value of x is greater than 30");  
}  
</script >  
</body>  
</html>
Output
Value of x is greater than 30

if else statement

The if else statement is use for either condition is true of false. Syntax
if(condition)  
{  
// set of statements  
}  
else  
{  
// set of statements  
}
Example
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var x=20;  
if(x%2==0){  
document.write("x is even number");  
}  
else{  
document.write("a is odd number")  
}  
</script>  
</body>  
</html>
Output
X is even number

If else if

It checks the content only, if expression is true from several expressions. if else if statement is an advanced form of if else statement. Syntax
If (condition1)  
{  
Statement(s) to be executed if condition 1 is true  
}  
else if (condition 2)  
{  
Statement(s) to be executed if condition 2 is true  
}  
else if (condition 3)  
{  
Statement(s) to be executed if condition 3 is true  
}  
else  
{     
Statement(s) to be executed if no condition is true  
}
Example
<!DOCTYPE html>  
<html>  
<body>  
<script>  
var x=50;  
if (x==10){  
document.write("x is equal to 10");  
}  
else if(x==50){  
document.write("x is equal to 50");  
}  
else if(x==30){  
document.write("x is equal to 30");  
}  
else{  
document.write("a is not equal to 10, 50 or 30");  
}     
</script>  
</body>  
</html>
Output
x is equal to 50