Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary

I am trying this code in atom with ESLint

Code snippet:

$scope.IsVisible = false;
$scope.ShowHide = function () {
  $scope.IsVisible = $scope.IsVisible ? false : true; // error
};

Got this ESLint error: error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary

Tried with these solutions solution 1 and solution 2, but error is not fixed. Furthermore, code works fine in editor without ESLint.

like image 555
manvi77 Avatar asked Mar 09 '17 21:03

manvi77


2 Answers

Try the good way =). No need to use statements in this case.

$scope.IsVisible = !$scope.IsVisible;
like image 174
lin Avatar answered Oct 18 '22 17:10

lin


In my case, I wanted to assign a value to my boolean, according to a variable that was either an object or null.

As kylesimmonds stated, the double bang !! operator can be used.

const myObject: SomeObjectType | null;
...

const isObjectExistent: boolean = !!myObject;
/*
  instead of:
  const isObjectExistent: boolean = myObject? true : false;
*/

Check: What is the !! (not not) operator in JavaScript?

like image 1
treecon Avatar answered Oct 18 '22 17:10

treecon