Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left Hand Side Conditional Assignment

Tags:

javascript

Is this possible in javascript?

let a, b; // Both objects, eg vectors
let bl = false; // This is a bool that could be true or false

// Is this possible, or how could it be done
(bl ? a : b) = {x: 5, y: -2, z: 3};

Ie, I want to set either a or b to this vector conditionally depending on bl.

Or is the only way:

let tmp = {x: 5, y: -2, z: 3};
if(bl) a = tmp;
else b = tmp;

I just feel there should be a more elegant way of doing this.

like image 501
Rewind Avatar asked Jun 27 '26 12:06

Rewind


1 Answers

Well in a way it is but it's just a short hand of what you wrote as an alternative.

let a, b;
let bl = true;

let obj = {x: 5, y: -2, z: 3}

bl ? a = obj : b = obj;
like image 164
János Aracsi Avatar answered Jun 29 '26 00:06

János Aracsi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!