180

JavaScript:

> "5" - 2
3

> "5" + 2
"52"

Enjoy coercion.

Comments
  • 8
    well what did you expect
  • 16
    @RazorSh4rk an error of some sort is what I would expect.

    Edit:
    Forget this was javascript I take my words back I don't expect anything
  • 12
    I keep my politics and my honest opinion of JavaScript to myself. I don't want people I like getting upset.
  • 8
    Inconsistent but it kinda makes sense...
  • 2
    What's wrong with it? Makes sense to me.
  • 21
    The "+" operator on strings is the concatenate operator that will connect two strings (or in this case string and int). There is no "-" operator for string so I guess it makes sense that JavaScript assumes it's a arithmetic operation
  • 5
    In some languages, you can deduct 2 strings. You then get the Levenshtein distance between the 2 strings: the smallest amount of single letter operations to transform string a to string b @ElCapitan
  • 3
    Yeah, Javascript sucks. People do amazing things with it indeed, but it still sucks.
  • 17
    I won't be surprised the day they come up with a "you know what I mean" operator, for operations like

    “5” ? 1 = “Six"
  • 6
    I do Java. I try to do JavaScript thinking I got this and it makes me want to throw My monitor out of the window, stick the keyboard Into the ass of the neighbor, use mouse as a flail and kill everyone and use the laptop as a campfire. I still try. I like to see the world burn.

    Then to stabilize my mind I do some Python.

    =D
  • 1
    Precisely the sort of thing to expect from a dynamic language. I vastly prefer static type systems, especially Haskell's.
  • 2
    I love javascript
  • 6
    This just in: people using tool wrong get angry that tool does things wrongly...
  • 1
    @DServy probably the case
  • 4
    @xios if you've assumed type safety in any dynamic typed language, you've done it wrong.
  • 2
    What's wrong? Doing math on strings didn't work quite the way you expected?
  • 0
    @DServy Java enforces typesafety. And if we look at the first post its not about type safety. It's about JS overloadin +. Its either addition or concatenation. Because first attribute is string it selects concatenation.
  • 1
    @xios yeah, but '+' overloading is common with many dynamic typed languages. You wanna get mad at JavaScript? Open up the js array class type...
  • 0
    @DServy Not mad. Just not enough knowledge/experience with it. Working on it. Learning is sometimes painful.
  • 1
    @xios agreed! I guess I think of js as a luxury since I spend way too much time in Apex.... My god.... It's a trainwreck
  • 0
    @DServy Never really looked at Apex. Quick glimpse looks like an iteration of Java with a .. WTF is that:

    Use the following syntax for creating a list:

    List<Integer> My_List = new List<Integer>();

    List <datatype> list_name
    [= new List<datatype>();] |
    [=new List<datatype>{value [, value2. . .]};] |;

    I feel your pain
  • 1
    @xios iT's CaSe InSeNsItIvE!
  • 0
    Not a bug but a feature.
  • 2
    I would expect an error when you subtract from a string...but...ok, I guess.
  • 2
    XD Only a JS dev would get it.

    second:
    a string "plus" a number should always be a string . comparing not equal values and/or types should always result in false. in parallel universe that's called logic .
    first: Hi , thanks for the post.
  • 3
    @MunzMan uh yeah I think we all understood this, but...thanks.
  • 0
    @MunzMan No, it's not called logic. It's called type coercion. Some languages call it implicit type conversion.

    Operators AND, XOR, NOT and OR is called logic.
  • 0
    I could be wrong but the simple explanation that come to my mind is : the substract operator has no implementation for string type, so it handle both as number. The + operator has an implementation for string, so it override the sum behavior.
  • 2
    @SpencerBeige the order of the bullet points I wrote is incorrect, I was making fun of JS async, that's what i meant by "Just JS dev would understand"
Add Comment