DEV Community

loading...

Discussion on: Simple concepts of Modern JavaScript

Collapse
couch3ater profile image
Connor Tangney

Akin to the ternary operator, if you just want to quickly evaluate something, and don't really care about the inverse situation, check out guards!

They're contextually similar to the ternary operator and I honestly have found more use-case for this in the past than ternaries:

//a variable to test with
let a = 'a';

//condition && result
a === 'a' && console.log('yes it does!');

Keep in mind, like the ternary operator, you should avoid complex logic in the block that is evaluated when your condition is met:

//a variable to test with
let a = 'a';

//condition && result
// !! THIS WON'T WORK !!
a === 'a' && (
  console.log('did this work?')
  console.log('no :(')
);
Collapse
pachicodes profile image