I don't want to substitute anything when arraying with a trinomial operator.

Asked 2 years ago, Updated 2 years ago, 385 views

 function test(a,b,c){
 a[b]=c%2===0? "Goose": here!!!!!
}

In the case of code like the one above, you must always write something to substitute after ":", but I don't want to substitute anything.
Null and undefined will replace it, so should I use if honestly now?

javascript

2022-09-30 22:05

3 Answers

No operands are optional to use the conditional operator (Reference.

If you don't want the value to change before and after execution, you can replace yourself.

 function test(a,b,c){
  a[b]=c%2===0? "Goose": a[b];
}

consta=["xue", "xue";

test(a,0,0);
test(a,1,1);

console.log(a);


2022-09-30 22:05

How about using Logical product

 function test(a,b,c){
  c%2===0&&(a[b]="Goose";
}

consta=["xue", "xue";

test(a,0,0);
test(a,1,1);

console.log(a);


2022-09-30 22:05

Since it seems impossible to say that you only do something when the conditions are true with the trinomial operator, I'll use the if statement


2022-09-30 22:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.