93

JavaScript logic.

Comments
  • 3
    It'd be

    1-+"1"
  • 4
    ParseInt to the rescue.
  • 23
    1 + "1" assumes string concatenation because the + operator can be concatenation or addition and since there is a string present it assumes concat is necessary.

    Using the - operator assumes substraction and it does its best to make it happen.

    Sure it's weird but it makes sense.
  • 1
    @Greggergalactic so when it comes to +, it adds the int to the char... I mean string.
    And when it comes to -, it doesn't concatenate... but why? Is there no way to do something like "reverse concatenation" (this is obviously a made up acronym BTW)
  • 6
    @demiko It doesn't, it will do the same if string is first. I suppose designers assumed that it is more common to concat string and other types without casting them, than it is to do numerical operations on non numerical types. From my own experience I find this to be true.

    They could have of course do some fancy checks to see if all values will evaluate to numerical types and choose to do numerical operation instead by I personally prefer that it is just "If one value is string and you are using string operator then it will be always string operator that is used".
  • 1
    @Greggergalactic No, that makes sense in normal speaking. THAT IS FUCKED UP IN PROGRAMMING.
  • 1
    Why not use & for concat and + for adding
  • 7
    @ctrlz someone needs to learn about bitwise operators.
  • 0
    You need to be a cat person to like javascript
  • 1
    @nicholai who? ;)
  • 1
    @paziek not to mention it is more likely you're trying to combine a number with a sentence or word or something since alpha text has to be in a string if it's not a variable/identifier and a number can be in either form. So it's probably safer to assume a number would be a literal number and anything in a string being used with the + operator is more likely being appended to the other item.
  • 1
    @ElsSnek can you tell me a scenario that you'd use "reverse" concatenation? - to my knowledge only works in expressions opposed to + is both concatenation and expressions because there isn't a real use for - as anything else.
  • 1
    @Greggergalactic
    Idk it was a product of brainstorming. Maybe it doesn't exist... just a thought :)
  • 1
    @ElsSnek yeah I mostly meant I can't think of a situation where it would be necessary.
  • 1
    @ctrlz or why not use ++ for concatenate
  • 2
    I hate javascript, but I also hate when people blame a programming language for writing shitty code where they just assume the language, and anyone reading the code, know how they intended to cast types.
  • 0
    @nicholai he is making a pun to BASIC...
Add Comment