Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
It's a short-circuiting operation. JS supports implicit conversion of reference types to booleans via the undefined/null evaluation. A side effect of this is this type of non-assignment comparion, due to how resulting values bubble to a point where they are either assigned, or hit global scope. In this case, it's just a discard.
-
Interestingly enough, Axel Rauschmeyer of the typescript team wrote a post on this.
https://2ality.com/2013/04/...
For anyone who didn't know, everyone on the Typescript team is fucking smart, and half of them it's not their first influential language. -
There are quite a lot of langues wich allow discarding the result of an expression.
The code in question is a conditional execution of someFunction if someString evaluates to true.
Obviously, that only makes sense if someFunction isn't pure - so expect it to alter some state somewhere.
I didn't do any JavaScript for years now - but i would have expected that someFunction may only be undefined if someString evaluates to false. -
hitko31485yI presume you come from Java / C#? C, C++, Python, PHP, Rust, GO*, ... all allow this kind of code.
* GO forces you to add a blank identifier (_) to those lines, to ensure it's done on purpose. -
cb21910195y@melezorus34 have seen the documentation several times now.
Sooo my guess would be that it actually uses the left-hand side to evaluate the right-hand function, which again does smth, as it has already been explained. But just ... why do it this way and not more explicit?😅 -
@SortOfTested I know and tweaked the way it works in my alternate FixedPoint class to operate well with numbers. Sad thing I didn't save it. MDN: https://developer.mozilla.org/en-US...
-
hitko31485y@cb219 Why? Because you can just remove the assignment part of some old code without changing the behaviour, and for someone looking at a diff it'll be much easier to see what you did.
-
hitko31485y@cb219 Yeah, but what if the line used to be var x = someString && someFunction(), and you don't need x anymore? If you just remove variable assignment the diff will clearly show what you did, however if you rewrite the whole thing someone will have to look deeply into the diff to see what functionality changed.
-
@cb219
It is pretty explicit already.
Also commonly used in PHP where it is done with the lower-precedence and operator:
$something and someFun(); -
@hitko
C# doesn't allow comparisons as statements, it doesn't support ref -> bool conversion. It relies on null coalescence and conditional for this. -
hitko31485y@cb219 Different domain, different approach... Java works in a structured environment where everything is meant to be explicit and predictable; languages like JavaScript are for environments with lots of simple logical operations and unpredictable external actions or input, and they're made to handle whatever gets thrown at them without crashing.
-
Ever wanted an ternary operator without an else?
Ever wanted to make something else if the previous thing didn’t result in something useful?
Binary operants on your way!
Not too uncommon for me, as I’ve written binary calculations on paper in electronics basics class, ugh -
@010001111 It’s an expression like anything else in js. It’s just in a different scope. Its in an evaluation scope, not assignment scope. Basically, an assignment is just a function call with their own evaluation scope
The question is, why you think this _shouldn’t_ work. -
cb21910195y@010001111 It's all fine now, javascript and all other similar languages have interesting functionalities that I wouldn't have expected. Just the sheer habit of Java experience, put to the test.😄
Related Rants
Javascript is my main tool at work right now, Typescript to be precise. It never ceases to amaze me, I guess as most of you.
What I've encountered recently still doesn't make sense to me but it seemingly works:
function a(){
someString && someFunction()
// other stuff ...
}
someString can be a string or undefined, someFunction can be a function or undefined.
How is that valid Javascript, without an assignment? Haven't found any explanation yet.
rant
how is that valid?
js
no assignment
boolean expression
always smth to learn