r/learnjavascript • u/starcrafter84 • Jan 12 '22
A warning to learners
JavaScript is written exclusively in camelCase. Be aware of this fact when reading/debugging your code because the syntax highlighter doesn’t pick it up and although everything looks right, it can all hinge on the first letter being wrong. (Source) spent 2 hours trying to debug a DOM call to document.getElementById(„elm“).InnerText, even putting it into jsfiddle and several other things to see what was wrong when all that was required was to change it to .innerText to solve the dilemma. Lesson learned and now +10xp. It’s tough coming to js when you are used to statically typed languages. /s
Kidding aside I am enjoying learning js/Frontend but I would be lying if I said I missed the capabilities of a good ide in a stricter language.
7
4
u/Coraline1599 Jan 12 '22
I know you have suffered, but now you have learned this lesson and because it was so awful, you will not make this mistake (or you will be able to fix it very quickly).
I have a similar tale of woe copying something that had converted dumb quotes into smart quotes.
1
7
u/Suchy2307 Jan 12 '22
Try TypeScript.
9
u/JazzApple_ Jan 12 '22
Don’t even need TS… VSCode has good autocompletion for JS.
-4
u/starcrafter84 Jan 12 '22
Here’s the sad thing. I was using vs code when I made the mistake. Forgot to press the tab button and suffered the consequences.
5
u/starcrafter84 Jan 12 '22
I knew this comment was coming. If the recommendation is that I change all my source files from .js to .ts and all will be well In the world then it would truly be a utopian existence. The Reality I believe is not quite the same. I am however excited to experience the bliss of typescript once i have served the obligatory suffering of the standard JavaScript inauguration.
1
u/Cheshur Jan 13 '22
If the recommendation is that I change all my source files from .js to .ts and all will be well In the world then it would truly be a utopian existence.
Pretty sure TypeScript would have pointed out your problem to you with out of the box configuration. If you don't want to use TypeScript then I highly recommend you familiarize yourself with a debugger because it would have cut down your 2 hour search to a couple minutes at worse.
1
u/starcrafter84 Jan 13 '22
I was using vs code to do it. I was tired, it had been a long day and I was focusing on another aspect of the code entirely, couldn’t figure out how to get the script to show up in the browser debug console, eventually copied it into site.js, set a breakpoint hit it and stepped through. Even although I could see the return value was Unassigned or effectively null I was suspecting the selector because it was dynamically created. So basically lots of different problems round in circles until I gave up on it for the evening. Came back next day and spotted it immediately. The general consensus here seems to be “use a linter”. I also want to start using unit tests on my js code but last time I tried to set the environment up it wasn’t particularly intuitive.
1
u/starcrafter84 Jan 13 '22
And BTW I’m not averse to using typescript. I just wanted to get enough xp of js before moving onto the superset of it. I’ve picked 1 front end language, 1 backend and 1 database and will stick with them until I’m proficient enough to work easily with them before moving on.
2
u/Dragonasaur Jan 12 '22
VSCode and Webstorm should have good syntax checking, and yeah TS with scripts/linters that'll prevent your build from completing since it doesn't pass error checks
2
u/EnlightWolif Jan 13 '22
As a learner, I'd like to ask the following (although it's kinda tangential).
When calling DOM elements, should I create variants or just operate by writing "document.getElementById" time and time again?
2
u/lovin-dem-sandwiches Jan 13 '22
Novice here. By variants, do you mean variables?
I use querySelector and store in a const for easy calls and legibility.
2
u/starcrafter84 Jan 13 '22
One day I got so annoyed with writing all the document.getelementbyid(“thing”).whatever that I started writing little short hand functions for it, then discovered that was exactly what jquery is doing.
3
u/lovin-dem-sandwiches Jan 14 '22 edited Jan 14 '22
Jquery is beyond my time. I’m glad I started at a point where Vanilla JS is polished enough that external libs isn’t a must have 😁
1
u/starcrafter84 Jan 14 '22
At this Point everything that is available is before my time. I do just want to learn as much as possible and try to only use vanilla tooling where possible. This not only forces me to learn the why but also know the how without being spoon fed a gigantic library
1
3
3
u/Cambuchi Jan 13 '22
A linter and properly set up vscode/webstorm should catch most errors of these types. Linters are made specifically to make your code conform to JS style guides.
2
u/shuckster Jan 13 '22
This is why I only reference API functions using dictionaries that contain all my typos.
2
1
-8
u/starcrafter84 Jan 12 '22
I think i spotted a swear word in your comment there. Documentation. What the IDE autocomplete don’t tell me, don’t exist. It is just jest though. Ignore my bitter old ass complaining, i want to learn new stuff but learnings hard when your a grumpy old shit.
77
u/senocular Jan 12 '22
FWIW, JavaScript is not exclusively camelCase. Functions, methods and properties often are:
But constructors use PascalCase.
Whereas constant properties will use capitalized snake_case
You'll also see all lowercase properties in the Web API for events
Use code completion/hints when available. Using TypeScript can help even more on top of that. And when in doubt don't be afraid to refer to the documentation.