Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Left Hand Side Conditional Assignment

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading