JavaScript getDate
Javascript Infinity
JavaScript undefined
JavaScript eval function
JavaScript uneval function
Javascript isFinite function
Javascript isNaN functions
Javascript parseFloat functions
Javascript parseInt functions
Javascript decodeURI functions
Javascript decodeURIComponent functions
Javascript encodeURI functions
Javascript encodeURIComponent functions
Javascript escape functions
Javascript unescape functions
Javascript Objects
Javascript Number
JavaScript Date object
JavaScript Math Object
JavaScript Array
Javascript Comments
JavaScript Data types
JavaScript special characters
JavaScript if condition
Javascript For Loop
JavaScript While Statement
JavaScript Switch Case statement
JavaScript Exception handling
JavaScript do while statement
JavaScript label statement
JavaScript break statement
JavaScript continue statement
JavaScript for in statement
JavaScript function
JavaScript Assignment operators
JavaScript Comparison operators
Javascript Arithmetic operators
JavaScript Bitwise operators
JavaScript Logical operators
JavaScript String operators
JavaScript Conditional operators
JavaScript typeof operator
JavaScript void operator
JavaScript toExponential functional
JavaScript toFixed function
JavaScript toPrecision function
JavaScript charCodeAt function
JavaScript concat function
JavaScript fromCharCode function
JavaScript indexOf function
JavaScript lastIndexOf function
JavaScript localeCompare function
JavaScript match function
JavaScript replace function
JavaScript search function
Javascript slice function
JavaScript split function
JavaScript substr function
JavaScript substring function
JavaScript toLocaleLowerCase function
JavaScript toLocaleUpperCase function
Javascript toString function
JavaScript trim function
JavaScript valueOf function
JavaScript pop function
JavaScript push function
JavaScript shift function
JavaScript unshift function
JavaScript sort function
JavaScript reverse function
3 Example(s) of JavaScript if condition
Description :
JavaScript if condition
is used to execute a statement if the condition is true, In our example, if the age is less than 18 then "You are not qualified for Voting" message will be displayed else "You are qualified for Voting" message will be displayed. See the code snippet for JavaScript If condition:
JavaScript if condition Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="dayGreeting()">Show the message</button>
<p id="sample"></p>
<script>
function dayGreeting() {
var age = 19
var message;
if (age < 18) {
message = "You are not qualified for Voting";
} else {
message = "You are qualified for Voting";
}
document.getElementById("sample").innerHTML = message;
}
</script>
</body>
</html>
Description :
In this example, We have a flag
variable with true as its value.
var flag= true; if(flag){ document.write("flag is true. "); }
It will return "flag is true" statement.
JavaScript if condition Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<script>
var flag= true;
if(flag){
document.write("<b>flag is true.</b>");
}
</script>
</body>
</html>
Description :
if the variable book is "maths" then it will print as "Maths Book", following condition will execute
else if( book == "maths" ){ document.write("Maths Book "); }
JavaScript if condition Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<script>
var book = "maths";
if( book == "history" ){
document.write("<b>History Book</b>");
}
else if( book == "maths" ){
document.write("<b>Maths Book</b>");
}
else if( book == "economics" ){
document.write("<b>Economics Book</b>");
}
else{
document.write("<b>Unknown Book</b>");
}
</script>
</body>
</html>
Please enable JavaScript to view the comments powered by Disqus.