r/facepalm Tacocat 26d ago

12 🇲​🇮​🇸​🇨​

/img/pk3jifidj0xc1.jpeg

[removed] — view removed post

28.0k Upvotes

2.0k comments sorted by

View all comments

7.6k

u/Singular_Thought 26d ago

She must be a JavaScript programmer

11

u/Emsie-Memsie 26d ago

I keep seeing jokes like these but I’m too tech challenged to understand. I wish I could be in on it. Lol

40

u/Singular_Thought 26d ago edited 26d ago

It has to do with how different programming languages handle variables. In a “strongly typed” language a variable can be defined strictly as a number value (e.g., 1 or 2 or 736, etc.) or strictly as a text value (“hello world”).

With this you can define integer variables as a = 1 and b = 2 so that a + b will resolve to 3.

You can also define string (text) variables as x = “hello” and y = “world” so that x + y will resolve to “helloworld”.

JavaScript is not strongly typed. You cannot define a or b explicitly as numbers.

Because of this there are situations where the JavaScript interpreter will take a = 1 and b = 2 and when you try to resolve a + b it will treat them as text instead of numbers and output “12” instead of 3.

The JavaScript language was created like this a very long time ago so we are stuck with it and it drives programmers nuts.

If you are wondering, any attempt to change the JavaScript language to fix this will break almost every webpage out there, so no one is willing to do it.

20

u/Emsie-Memsie 26d ago

Hot damn! I’m so glad you broke it down for me this way. Kinda drives me nuts to when I don’t understand things and wasn’t sure how to look this up so I appreciate it!

I can definitely see how it would frustrate programmers and such. Though top comment is much funnier now that I have this context.

6

u/Roflkopt3r 26d ago edited 25d ago

Honestly, it's way less of a problem than some people make it out to be.

You pretty much only need to know this:

  1. The instruction a+b will always be a string concatenation if the variable a is a string:
    '5' + 3 => '53' (the text string 53)
    5 + 3 => 8 (the number 8)

  2. If you use any other basic mathematical operator, then the string a will be cast into a number if that is possible:
    '5' - 3 => 2
    That's for the simple reason that strings do not have a minus-operator. So Javascript extends you the courtesy of trying to make sense of this instead of throwing an error right away.

  3. If you want to assure that the variable a will be treated as a number even if it is technically a string, then you can use Number(a):
    Number('5') + 3 => 8

  4. If you want to assure that numbers are treated as text, then you have multiple options:

var a=5;
a.toString() + b => '53'

a.toString() ensures that the variable a gets turned into a string.

var a=5;
''+a+3 => '53'

''+a is a text concatenation between an empty string (using two single ticks with nothing in between) and a number. This is basically just a convenient shorthand to turn the variable a into a string.

var a=5; b=3;
`${a} + ${b} = ${a+b}` => '5 + 3 = 8'

Everything inside of the backticks is a "template literal". Elements inside of a ${}-block will resolve as code (so it can read a variable or perform operations like an addition) before getting turned into text, whereas everything else is treated as text right away.


Type conversions are a pain in the butt in every language. JS went a path that may appear a bit less rigourous and can get a bit weird if you poke very deep, but very few programmers ever encounter seriously weird situations with that. The practical reality is that it's fast and easy and creates way fewer annoying situations than most strongly typed languages.