1
cb219
4y

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.

Comments
  • 1
    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.
  • 1
    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.
  • 2
    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.
  • 0
    Conditional operators' working principle:
    Or (||) usage: lefthand || righthand
    If lefthand is false, return righthand (which can be a conditional operator result)
    Otherwise return lefthand

    And(&&) usage: lefthand && righthand
    If lefthand is true return righthand;
    Otherwise return lefthand.
  • 1
    I 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.
  • 0
    @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?😅
  • 0
    @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...
  • 1
    @cb219 convert it to ```
    if (someString) someFunction();
    ```
    As a refactor.
  • 0
    @cb219 too bored to type more I guess.
  • 2
    @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.
  • 0
    @hitko hm, I guess personal taste. I like it more explicit. 😄🤷
  • 0
    @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.
  • 0
    @cb219
    It is pretty explicit already.
    Also commonly used in PHP where it is done with the lower-precedence and operator:
    $something and someFun();
  • 1
    @melezorus34
    I think you're replying to the wrong post.
  • 0
    @hitko
    C# doesn't allow comparisons as statements, it doesn't support ref -> bool conversion. It relies on null coalescence and conditional for this.
  • 0
    @hitko I come from Java.
    Old habits, I see ... 😅
  • 1
    @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.
  • 0
    @SortOfTested I think my brain needs to go into secure mode.
  • 0
    @hitko dude come on I wrote what is it explained as good as I could.
  • 1
    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
  • 0
    @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.
  • 1
    @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.😄
Add Comment