Conditional statements are used to perform different actions based on different conditions.

In general, when writing code, you always need to perform different actions for different decisions. You can use conditional statements in your code to do this.

In JavaScript, we can use the following conditional statements:

  • If statement – This statement is used to execute code only if the specified condition is true
  • if… Else statement – Executes code when the condition is true and other code when the condition is false
  • if… else if…. Else statement – Use this statement to select one of multiple code blocks to execute
  • Switch statement – Use this statement to select one of multiple code blocks to execute

 

Code:

<! DOCTYPEhtml>
<html>
	<head>
		<meta charset="utf-8">
		<title>if.. else</title>
	</head>
	<body>
		<p>Any time earlier than 10:00 will be greeted with a "good night"</p>
		<button onclick="myFunction()">Let me test</button>
		<p id="demo"></p>
		<script>
			function myFunction(){
				var x="";
				var d=new Date().getHours();
				if(d<10) {Copy the code