67

Is it possible to do something like this in JavaScript?

max = (max < b) ? b;

In other words, assign value only if the condition is true. If the condition is false, do nothing (no assignment). Is this possible?

codiac
  • 1,681
  • 4
  • 16
  • 29

11 Answers11

116

Don't use the ternary operator then, it requires a third argument. You would need to reassign max to max if you don't want it to change (max = (max < b) ? b : max).

An if-statement is much more clear:

if (max < b) max = b;

And if you need it to be an expression, you can (ab)use the short-circuit-evaluation of AND:

(max < b) && (max = b)

Btw, if you want to avoid repeating variable names (or expressions?), you could use the maximum function:

max = Math.max(max, b);
Jack Bashford
  • 40,575
  • 10
  • 44
  • 74
Bergi
  • 572,313
  • 128
  • 898
  • 1,281
16

An expression with ternary operator must have both values, i.e. for both the true and false cases.

You can however

max = (max < b) ? b : max;

in this case, if condition is false, value of max will not change.

marekful
  • 14,178
  • 5
  • 35
  • 56
  • I'm curious... If the condition is false, under the hood, will it run an operation at all? Or will it replace itself. – Brendan Jun 17 '20 at 18:12
  • I believe there are always to operations: eval the condition, then assign one of two different values. Of course the eval may result in many operations in itself. – marekful Jan 19 '21 at 12:13
9

You can just set max to itself if the condition is false.

max = (max < b) ? b : max;

Or you can try using the && operator:

(max < b) && (max = b);

Or to keep your code simple, just use an if.

if(max < v) max = b;
gen_Eric
  • 214,658
  • 40
  • 293
  • 332
6

I think ternary is more suitable try this

(max < b) ? max = b : '';
Aditya Jain
  • 61
  • 1
  • 4
4

There isn't a specific operator that isn't the ternary operator, but you can use it like this:

max = (max < b) ? b : max;
Katie Kilian
  • 6,539
  • 5
  • 41
  • 62
2

I think a better approach could be

max = Math.max(max, b)
dpolicastro
  • 1,177
  • 1
  • 10
  • 17
2

You can do something like this:

(max < b) ? max = b : ''
Gaurav
  • 1,309
  • 1
  • 19
  • 27
2

you can try:

(max < b) && (max = b);

look at this example:

let max = 10;
let b = 15;
(max < b) && (max = b)// this will be true
console.log("max=", max);

let maxx = 10
let bb = 5;
(maxx < bb) && (maxx = bb)// this will be false
console.log("maxx=", maxx);
Dory Daniel
  • 736
  • 12
  • 20
1

Instead of the ternary operator, you could use Logical AND assignment (&&=).

Basic examples

let output = false;
output &&= true;
console.log(output)

Reassignment is not possible because output has been defined as false.

const b = 10;
const max = 1;
let sum = max < b;

sum &&= 'new value';
console.log(sum);

Returns a string, because the variable sum has been defined as true.

Reza Saadati
  • 4,172
  • 4
  • 24
  • 58
0

There was no example of ES6, we can use in this way:

let list = [{id: "abc", name: "test1"}, {id: "xyz", name: "test2"}]
let selectedData = {};
      list.some((exp) => {
        return (
          exp.id == "xyz" &&
          ((selectedData = exp), true)
        );
      })
console.log(selectedData);

Added description after a negative vote: Just adding a description to explain my solution. If you have an array of objects and you want if only one condition satisfies then we can use some. It will set the selectedData when the id == "xyz". As asked in the question, this will assign value only if the condition is true otherwise selectedData will be empty.

KushalSeth
  • 1,901
  • 1
  • 15
  • 25
0

The ternary operator is used where we have a minimum of two possible outcomes.

let no = 10;
let max = 20;

max = no > max ? no : max

if you only want to use if, not else, then I recommend you to use if not the ternary operator.

let no = 10;
let max = 20;
    
if (no > max){
   max = no
}

if you want to use ternary operator, but don't want to use else part, then you may use this, but this won't be useful in every problem

let no = 10;
let max = 20;

no > max ? max = no : 0

here we are assigning value to a max variable only when the condition is true, otherwise, we are not doing anything

Rizwan
  • 28
  • 6