दोस्तों इस chapter में हम JavaScript If Statements के बारे में जानेंगे तो चलिए शुरू करते है. JavaScript में if statement की आवश्यकता तब होती है जब हम किसी code को कोई fixed position संतुष्ट होने पर execute करना चाहते हैं।
JavaScript में If Statement का उपयोग कोड को executed करने के लिए किया जाता है. जब condition 'true' है तब if statement execute होता है, और अगर condition false है तो if statement execute नहीं होता है।
<!DOCTYPE html> <html> <body> <p>Display "Hello My friend!" if the hour is less than 14:00:</p> <p id="demo">Good Morning!</p> <script> if (new Date().getHours() < 14) { document.getElementById("demo").innerHTML = "Hello My friend!"; } </script> </body> </html>
Display "Hello My friend!" if the hour is less than 14:00:
Good Morning!
JavaScript में if-else स्टेटमेंट का उपयोग कोड को executed करने के लिए किया जाता है. चाहे स्थिति सही है या गलत है if else statement में जब condition true होती है तब if का statement execute होगा और अगर condition false होती है तो else का statement execute होगा।
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JavaScript IF...Else Statements</title> </head> <body> <pre> <script type="text/javascript"> var c = 15, d= 20; if (c > d) { document.write("c is bigger than d"); } else { document.write("c is smaller than d"); } </script> </pre> </body> </html>