-11

Here's a perfect illustration I saw.

&&

if (num > 5 && num < 10) {
return "Yes";
}
return "No";

||

if (num > 10 || num < 5) {
return "No";
}
return "Yes";

What's the difference between these two?

  • 4
    || - any condition true - true. && all conditions true- true. – Suresh Atta Mar 03 '17 at 12:04
  • 1
    And & Or. Basic operators. – MX D Mar 03 '17 at 12:04
  • 1
    Hmms, basic stuff. There are multiple answers to this on this website, such as: http://stackoverflow.com/questions/41176040/javascript-logical-operators-and-vs-or. But nonetheless welcome at SO. [MDN](https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Operators/Logical_Operators). – Mouser Mar 03 '17 at 12:05
  • var num = 6; if (num > 5 && num++ < 10) { // do something } console.log(num); var num = 6; if (num < 10 || num++ < 5) { // do something } console.log(num); - As said by everyone they are the basic and & or operators. However, copy this and see a difference – Nikhil Aggarwal Mar 03 '17 at 12:06

2 Answers2

0

They are AND (&&) and OR (||) operators.

In your first example the response will only be "Yes" if num is greater than 5 AND less than 10, so any value for num between 5 and 10 (exclusively) will return "Yes".

In your second example "No" will be returned if num is greater than 10 OR less then 5, so any value between 5 and 10 (inclusively) will return "Yes".

Kallum Tanton
  • 802
  • 7
  • 22
0

To make the expression

num > 5 && num < 10

negative,

!(num > 5 && num < 10)

you need to apply De Morgan's law

!(num > 5) || !(num < 10)

and reverse the condition inside of the parens

num <= 5 || num >= 10

In your question you have the expression with logical Or ||

num > 10 || num < 5

which is, despite the order, obviously not the same as the above expression. The result of comparing values is not the same.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392