0

JavaScript (x, y)

Why : Var x = (y1, y2) is equal to y2 In JavaScript

31st Jan 2020, 10:37 AM
Oussama Messaoudi
Oussama Messaoudi - avatar
15 Answers
+ 5
It is a bad practice, because of low readability
31st Jan 2020, 10:37 AM
Gordon
Gordon - avatar
+ 5
I'd like to call in my specialist for bad practices, undefined behaviours and such, ~ swim ~ (if you don't mind, I'm being a pest ^^).
31st Jan 2020, 10:55 AM
HonFu
HonFu - avatar
+ 5
What makes it a bad practice, then, if you know exactly how it works?
31st Jan 2020, 11:11 AM
HonFu
HonFu - avatar
+ 4
a good use case is : a && (b, c) means : if (a) { b; return c; }
31st Jan 2020, 1:45 PM
Oussama Messaoudi
Oussama Messaoudi - avatar
+ 3
Is that a similar behavior to C, where you can with your left hand crement some values while you're storing a value with the right one? var n = (++x, --y, z);
31st Jan 2020, 10:48 AM
HonFu
HonFu - avatar
+ 3
using this syntax in REACT is a pretty and simple thing ✊🏾 Schindlabua if u used react before u will understand 🤞🏼
31st Jan 2020, 2:29 PM
Oussama Messaoudi
Oussama Messaoudi - avatar
+ 3
Oussama Messaoudi Seeing how you argue with Schindlabua, may I refer you to read this article, which is a programmer's heartfelt advice on why you shouldn't write "clean" code based on his real work experience. https://www.sololearn.com/post/223948/?ref=app https://code.sololearn.com/WKKkpq0efxai/?ref=app
31st Jan 2020, 3:30 PM
Gordon
Gordon - avatar
+ 2
why using this syntax ? what is the benefit ???
31st Jan 2020, 10:51 AM
Oussama Messaoudi
Oussama Messaoudi - avatar
+ 2
HonFu It's bad practice because there's always a cleaner alternative. var n = (++x, --y); parses much much easier if you write it as ++x; --y; var n = y;
31st Jan 2020, 12:38 PM
Schindlabua
Schindlabua - avatar
+ 2
Oussama Messaoudi that is pretty ugly :/ I mean, it's clever, but after a couple of hours of staring at code you don't really have the energy for "clever" code. It takes 5 seconds to write it more readably, and it also takes 5 seconds to read the shorter code; BUT you have to read the code 100 times and only write it once, so just go for the if :P
31st Jan 2020, 2:23 PM
Schindlabua
Schindlabua - avatar
+ 1
why ?
31st Jan 2020, 10:47 AM
Oussama Messaoudi
Oussama Messaoudi - avatar
+ 1
why is that syntax correct ?
31st Jan 2020, 12:50 PM
Oussama Messaoudi
Oussama Messaoudi - avatar
+ 1
The comma operator is a close cousin of the && and || operators. All three operators will return the last expression they evaluate
31st Jan 2020, 3:17 PM
Mohamed Atiea
Mohamed Atiea - avatar
+ 1
It is not a good practice because the application or requirement of client (you) the practitioner will face an ambiguous problem or take more time to resolve the equation code and deliver the application Please note that the requirement should be clear then only the client will get ouput what they are expecting
31st Jan 2020, 8:17 PM
sree harsha
sree harsha - avatar
0
Oussama Messaoudi I think the comma is mostly there so we can do many assignments like a = 1, b = 2, c = 3;
31st Jan 2020, 1:21 PM
Schindlabua
Schindlabua - avatar