Example

Logical Operators

HTML

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width">
	<link name="favicon" type="image/x-icon" href="https://skillzam.com/uploads/system/e353194f3b7cd1b75d69c3ab88041985.png" rel="shortcut icon" />
	<title>Logical Operators</title>
</head>
<body>
<!--
	Logical operators are used to test for true or false.
		Operator	Description
			&&	    logical and
			||	    logical or
			!	    logical not
-->    
	<h1>Demo: Logical Operators</h1>

	<div>
		<h2>Logical and (&&) demo: Example on Wonderword</h2>
		<p id="para1"></p>
	</div>

	<div>
		<h2>Example to combine all logical Operators: &&, || and !</h2>
		<p id="para2"></p>
	</div>


	<!-- JavaScript code Starts here -->
	<script>

		/************************************************/
		// JavaScript "logical and" Operator (&&)
		// Example on Wonderword
		//
		const wonderWord = "$killzam9"

		// Below if condition (true && true) is true
		if (wonderWord.length >= 9 && wonderWord.indexOf(' ') === -1) {
			document.getElementById('para1').innerHTML =
			wonderWord + 
			" is a perfect wonderword!";
		} else {
			document.getElementById('para1').innerHTML =
			wonderWord + 
			" is an incorrect wonderword!";
		}


		/************************************************/
		// JavaScript Logical Operators (&&, || , !) :  
		// Example to combine  && and ||
		//
		const percentScore = 55;
		if ( !(percentScore >= 50 && percentScore < 90 || percentScore > 90) ) {
			document.getElementById('para2').innerHTML =
			"You have scored less than 50%. Please score well!";
		} else {
			document.getElementById('para2').innerHTML =
			"You have scored well. Keep it up!";
		}

	</script>
	<!-- JavaScript code Ends here -->

</body>
</html>

Output

View original