Logical Operators

In this example below you will see how to do a Logical Operators with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by milstanyu, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright milstanyu ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Logical Operators</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/milstanyu/logical-operators-ZbJaeK */
var truth = true;
var falsehood = false;
var firstWord = "Hello";
var secondWord = "World";

// && - AND

console.log(truth && truth);                    // true (obe su true)
console.log(truth && falsehood);                // false
console.log((3 < 4) && ("apple" < "banana"));   // true (stringovi se uporedjuju po prvom slovu)

console.log(firstWord && secondWord);           // kao rezultat daje vrednost drugog vara (oba su true)




// || - OR

console.log(falsehood || falsehood);            // false
console.log(truth || falsehood);                // true (jedan je true)

console.log(firstWord || secondWord);           // kao rezultat daje vrednost prvog vara (koji je true)




// ! - NOT

console.log(!truth);      // false
console.log(!falsehood);  // true
console.log(!(3 < 4));    // false




// !! - dupli NOT - pretvaranje var-a u true ili false

console.log(!!firstWord);   // true





// redosled izvrsavanja logickih operacija

console.log(truth && falsehood || truth);    // true
console.log(truth || falsehood && truth);    // true
console.log(truth && falsehood || falsehood);    // false

Comments