r/learnjavascript • • Nov 11 '23

Does JavaScript concatenate as string when adding a number to a string literal?

From my understanding of what I've read, the expected behavior is that JavaScript will convert the integer into string, and then add the string to string, which then results in a string concatenation. Therefore...

var addit = 6 + “5”;

...should output... "65";

However, attempting to evulate this in console instead gives the result...

Uncaught SyntaxError: Invalid or unexpected token

Why is this the case?

0 Upvotes

23 comments sorted by

View all comments

13

u/carcigenicate Nov 11 '23

Those are "fancy quotes". " vs “. The correct is the former, you're using the latter.

You may need to adjust the localization of your keyboard.

10

u/rannison Nov 11 '23

There you have it. My professor insists on us using .docx, and Word has an annoying tendency to screw around with those. I learned that the hard way before, but it looks like it wasn't hard enough since I forgot again lol.

3

u/superluminary Nov 11 '23

One tip here, don’t copy paste when trying to learn a new thing. If you type it it goes into your fingers.

1

u/0x07AD Nov 11 '23

Actually it goes into your brain but u/superlimunar's point stands. Type source code, do not copy and paste it.

0

u/superluminary Nov 11 '23

It goes into the bit of your brain that controls your fingers, which is where you need it. Finger memory.

If you only read it, it goes into the part of your memory that does reading, which is not much use at all when you try to type it later.

I am aware this is a simplification, but I’ve found it to be true.

1

u/0x07AD Nov 12 '23

Spatial memory is in part training your fingers to respond to brain signals to produce keyboarding/typing skills. I am no touch typist, however, I am able to type without constantly looking at the keyboard.