grldtcsn
grldtcsn

Reputation: 49

How boolean condition works in javascript?

I am creating same if else function on my program. And I want to make it more short but same logic.

if(!checkStringInput("Test", 14)){
     elementHasError($element);
} else {
     elementIsOk($element);
}

I am thinking this boolean condition can be a shorter one to:

CONDITION ? FUNCTION1; : FUNCTION2;

Thanks for the help!

Upvotes: 1

Views: 85

Answers (4)

Zach Shallbetter
Zach Shallbetter

Reputation: 1921

You seemed to have it correct. Were you looking for something else beyond the terniary operation?

!checkStringInput('Test', 14) ? elementHasError($element) : elementIsOk($element);

Upvotes: 0

user703016
user703016

Reputation: 38005

Probably the shortest possible:

(checkStringInput("Test", 14) ? elementIsOk : elementHasError)($element);

Upvotes: 3

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21376

!checkStringInput("Test", 14)? elementHasError($element): elementIsOk($element);

Upvotes: 0

Sachin Jain
Sachin Jain

Reputation: 21852

Have you tried this

!checkStringInput("Test", 14)) ? elementHasError($element) : elementIsOk($element);

Upvotes: 3

Related Questions