Logo

JavaScript: Negation

Along with the logical operators AND (&&) and OR (||), the "negation" operation is often used. It flips a boolean value to its opposite. In JavaScript, negation corresponds to the unary operator !:

!true;  // false
!false; // true

For example, if you have a function that checks whether a number is even, you can use negation to check for oddness:

const isEven = (number) => number % 2 === 0;

isEven(10);  // true
!isEven(10); // false

We simply added ! to the left of the function call and got the opposite result. Negation lets you express the intended rules in code without writing new functions.

Double negation

But what if you write !!isEven(10)? Surprisingly, the code works. In logic, double negation is equivalent to no negation at all:

isEven(10);   // true
!isEven(10);  // false
!!isEven(10); // true

Combining with && and ||

! can be combined with && and ||. Among the logical operators, negation has the highest precedence, so it is applied first:

!true || true;   // (!true) || true   => false || true  => true
!true && false;  // (!true) && false  => false && false => false

Parentheses change the order of evaluation:

!(true || true);  // !true  => false
!(true && false); // !false => true

A practical example: a function checks whether a driver can get behind the wheel — a license and sobriety are required:

const canDrive = (hasLicense, isDrunk) => hasLicense && !isDrunk;

console.log(canDrive(true, false)); // => true  (has a license, sober)
console.log(canDrive(true, true));  // => false (has a license, but drunk)
console.log(canDrive(false, false)); // => false (no license)

De Morgan's laws

When working with complex logical expressions, you sometimes need to invert them or rewrite them in an equivalent, more readable form. For this there are De Morgan's laws — two rules that describe how negation distributes over a compound expression:

!(a && b)  ===  !a || !b
!(a || b)  ===  !a && !b

The first law: the negation of a conjunction equals the disjunction of the negations. Let's check:

!(true && false);     // !false => true
!true || !false;      // false || true => true

The second law: the negation of a disjunction equals the conjunction of the negations:

!(true || false);     // !true => false
!true && !false;      // false && true => false

In practice, De Morgan's laws help simplify conditions. For example, instead of !(isAdmin || isModerator) you can write !isAdmin && !isModerator — which reads as "not an administrator and not a moderator".

Instructions

Write two functions:

  1. isPalindrome(str) — returns true if the string is a palindrome (reads the same in both directions).
  2. isNotPalindrome(str) — returns true if the string is not a palindrome.
isPalindrome('level');    // => true
isPalindrome('hello');    // => false
isNotPalindrome('level'); // => false
isNotPalindrome('hello'); // => true

Use the .split(''), .reverse(), .join('') methods to reverse the string.

Tips

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!