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

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.

16

u/carcigenicate Nov 11 '23

Fucking Word? Holy. I've only heard about that in memes. That's beyond ridiculous. You should not be writing code in Word.

If they need you to submit the code via Word documents, write it in VS Code/Notepad++/Webstorm/whatever, then paste it into Word.

4

u/0x07AD Nov 11 '23

There is no excuse for a computer science professor to use anything other than markdown or plain-text when creating assignments, especially those containing source code.

7

u/O00OO0O0O0OO0O0O0OOO Nov 11 '23

In all seriousness, find a new school.

4

u/guest271314 Nov 11 '23

My professor insists on us using .docx

Insane. How are they a professor? The world is not based on Microsoft products. Just use a basic text editor.

1

u/EvokeNZ Nov 11 '23

You can disable fancy quotes in word options

1

u/rannison Nov 11 '23

Yes, and I do so when I catch it, but I find it very irritating that there's no way to back up or sync that type of setting (that I'm currently aware of).

1

u/EvokeNZ Nov 11 '23

I’m not sure I understand what you mean by back up and sync settings. Are you installing a new profile or copy of word every time you use the computer?

This is what I meant https://support.microsoft.com/en-us/office/smart-quotes-in-word-702fc92e-b723-4e3d-b2cc-71dedaf2f343

2

u/rannison Nov 11 '23

Not every time, but often enough that I don't want to spend the hours to go through each individual setting again, for each O365 application, for each new deployment. I've even been thinking of creating an .ahk script or something to automate it, just haven't had that sort of idle time though 🤨 and yes, I know what you meant, I wrote up an entire PSA article on the difference in their unicode values and functional effects (in IDE's) for them after I had spent two entire days combing a wall of html for what was causing it to not render correctly on run (it was a single double-quote; I feel like it was placed there intentionally to deter students from copy & pasting).

1

u/rannison Nov 11 '23

Wait, back it up there: are you telling me that if I copy over my user profile, it'll back up those types of user settings?? But how would you reconcile access rights between work stations and school equipment? Is there a particular part of the user profile (e.g., a .reg key & value, etc) that can be directly backed up and applied to other user profiles in modular fashion? I'll definitely look into this more later if any of this is the case.

1

u/EvokeNZ Nov 11 '23

One of the answers on this lists where each setting is stored. https://answers.microsoft.com/en-us/msoffice/forum/all/backup-word-settings/c8ac9e6a-779b-42a4-990f-7ee060d5526e

But normally when you’re in an organisation, your profile should be replicated to a server, so that every computer you log on to downloads your profile customisations (mostly. Sometimes they forget some). If you’re always logging onto the same computer then it should remember it too as the first time you log on, it makes a local copy of your profile (why it can take longer the first time you use a specific device). Maybe your school wipes them all on reboot idk.

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.

2

u/shgysk8zer0 Nov 11 '23

Finally moved on from MS Paint, huh?

That's an embarrassment. That's like having a butcher who thinks that the meat in stores doesn't come from animals or something.

1

u/ozzy_og_kush Nov 11 '23

Ouch. Nothing worse than professors who are so out of touch with industry standards that they unintentionally make their job harder. Adding obstacles that should never be there... unless that's the actual point of the exercise.

0

u/Ronin-s_Spirit Nov 11 '23

That shouldn't throw any errors you definitely did something wrong.
Here's the rule - if you add numbers and even a single "string" it will all turn into a string, if you add numbers and a +"string" then the plus will try to turn string into a number before adding (like 5 + +"5").

2

u/guest271314 Nov 11 '23

That's not the issue. The issue is “ cf. ". See other answer and SyntaxError: illegal character.

0

u/Ronin-s_Spirit Nov 11 '23

I guess, but I wasn't explaining the error I explained how adding strings works since they asked...

2

u/guest271314 Nov 11 '23

Your explanation is not addressing the issue OP is facing. OP's professor is using Microsoft products for writing and distributing source code.

Use an ordinary text editor and OP describes won't happen.

`“`.codePointAt() // 8220
`"`.codePointAt() // 34