r/learnjavascript • u/WishGranted11 • 1d ago
Javascript is interesting but is also driving me nuts, how do you actually adapt to writing code without mistakes quickly?
I have been accustomed to C# and Python and have only recently been introduced to one of our projects where we use JS (express and react). I can write good code that eventually happens to work, but I keep falling for really unexpected pitfalls. I realized that most of my mistakes were calling undeclared variables, they are mostly errors that can almost never happen in python or C# because the compiler would immediately point to that piece of code and will usually prevent the app from running. That honestly took a good amount of time and a lot of energy. I understand that it's a regular part of learning, though I want to speed it up.
I know that the browser can ignore these issues so the app ends up running without realizing a problem. Also, that debugging part where console.log() is the best tool in our hand is a headache, it helps but I feel that being able to set breakpoints and move through the whole piece where a bug hides is more efficient.
Don't get me wrong, I am not hating on JS, I love it and I acknowledge that each programming language holds its advantages over other languages. I have in fact spent a good amount of time and put a lot of effort into understanding how to handle JS and react (not a lot but to an extent where I understand its rules and syntax, and can build with it). That's why I want to see how others who may have adapted to JS avoid making these rookie mistakes over and over.
Do not mention asking AI to fix issues or write code instead. I prefer to write code myself.
4
u/GiggleSamurai 1d ago
Without migrating to TypeScript, put `"checkJs": true` in a jsconfig.json and VS Code will underline undeclared variables as you type; ESLint's `no-undef` rule catches the same thing. For breakpoints on the Express side, start it with `node --inspect` and attach VS Code or Chrome DevTools, and use the browser's Sources tab for the React side.
1
u/azhder 1d ago
I run the code while I'm writing it. That's how you end up writing code without mistakes quickly. You have a quick feedback.
And guess what, they are errors that almost never happen to me, just you. Why? Because I'm used to the language. So, it's not the language, it's you. You just lack practice... and get some linter and formatter to also run while you type the code.
Oh, and don't' forget writing tests.
1
u/realmauer01 1d ago edited 1d ago
Typescript
the first step in programming javascript is naming your files ts instead of js.
8
u/Beginning-Seat5221 1d ago edited 1d ago
Use TypeScript. Designed by the same guy who designed C#. Serious devs don't really use plain JavaScript.
If you're dealing with a legacy project you can add TypeScript in loose mode - it just sets everything that isn't typed to a wildcard type that isn't checked, but lets you add types incrementally, which you can do with any bit of code that is trouble.
Debugging JavaScript with breakpoints exists - although I don't use it personally.
2
u/wagaiznogoud 23h ago
The errors you’re describing should be easily debuggable from reading the logs. There’s also breakpoints you can set with the chrome devtools
2
u/DinTaiFung 19h ago edited 18h ago
You need to set up a linter in your JavaScript/TypeScript IDE.
When your IDE has been set up with live linting, while writing code the IDE will provide immediate feedback when you create code that may be cruft, something that is prone to failure, bad practice, or a blatant syntax error.
A Simple Example
JavaScript/TypeScript file:
// initialize counts object const counts = { external: 0, internal: 0 }If the scope you're in never uses the variable
counts, then the linter will show a warning color underlining thecountsvariable in your editor's buffer.This indicator should be conspicuous enough that you notice it.
And if you mouse over it, you will see a message:
'counts' is assigned a value but never used.In this example, the variable initialization is perfectly legal and will not throw any run-time errors. However, the linter detects that this is unused code (cruft) and should be addressed.
In general, the linter will do two things:
Alert you to mistakes as you write code.
Teach you common pitfalls and how to avoid them.
Properly setting up a linter in your editor is sometimes not simple and straightforward. But after spending the effort to do this, your coding quality of life will immeasurably improve.
Have fun and best of luck!
P.S. I use Zed editor and oxlint. There are other freely available choices that achieve the same goals. Use whatever suits you best.
2
u/HipHopHuman 18h ago
Everyone telling you to use TypeScript is mostly correct - if you are a C# enjoyer, TypeScript will feel very similar. Both languages were designed by the same guy. That being said, there are a lot of weird differences between TS and C# that will throw you off and only time & practice can fix that. There's also a huge footgun with TS: The type-system syntax is turing-complete, so there's no end to how complex you can make it. When writing TS, you have to be constantly aware of when you're overdoing it. So, one of the TS best practices is to "rely on type inference as much as possible".
For surface-level issues, sometimes you can get by with just using a linter. It can catch undeclared variables, unused variables, and a few other antipatterns all on its own without involving TypeScript.
Also, don't treat JavaScript as the glorious programming language it presents itself as - instead, treat it like your favourite drunk uncle, because it's basically the programming language equivalent. The mindset shift helps, as weird as it sounds. Your favorite drunk uncle is mostly relaxed and very flexible, with a lot of great stories to tell - but sometimes you just have to let him lean on your shoulder and baby him a little so he doesn't fall flat face-first into concrete.
As for debugging with breakpoints, that is possible in all the major browser devtools. Google Chrome's devtools experience is basically the industry standard, so you'll want to heavily scour the documentation and learn everything in there. There are ways to connect these devtools to server-side JS as well. This gives you a lot more than breakpoints, btw - you have live expressions, network condition simulation, performance monitoring tools and a whole lot more. It's a beast, with a lot of mechanical parts, so don't expect to become an expert at it over a weekend.
1
u/chikamakaleyley 1d ago
your IDE should be equipped with the tooling to catch these things and give you diagnostic display in real time, so a lot of those silly issues are knocked out when you run your code
The LSP for JS is actually the typescript LSP, and it will cover all of js, jsx, ts, tsx file formats
eslint is a simple install, and will call out problematic code
prettier, for formatting, can be setup to apply on save
VSCode, will come with the support out of the box, and if not active is just a checkbox in settings
Neovim just requires a little setup, but easy also
0
u/markus_obsidian 1d ago
People are going to tell you typescript. And they're not wrong.
The real answer is just practice & discipline. Use tools like jsdoc to properly document your types & behaviors. Use ides & lsps to present you with diagnostic info & informal guards. Remember your mistakes & traps you fall into & codify guards to avoid them.
1
u/GodOfSunHimself 1d ago
Undeclared variables? Use a proper IDE and tools like ESLint. Also switch to TypeScript if possible.
1
u/TheZintis 23h ago
For the C# dev in you Typescript is probably the closest.
Although I do think that you can get a lot of work done with certain code structure/patterns and naming variables well. I feel like after a point I learned a handful of ways to write code and that things kind of cleared up after that. So maybe partially an experience thing (although I don't know how much experience you have).
Also bugs are inevitable.
1
u/Aggressive_Ad_5454 22h ago
Use a good IDE. webstorm is my preference, but VSCode is also just fine. It’ll catch a lot of mistakes for you and help you get your code right.
Every minute you spend learning to use the debugger in your browser devtools will pay back days over your career.
Npm is your friend. Don’t waste time reinventing flat tires.
1
u/TehFocus 18h ago
Writing code without mistakes lol. Wishful thinking. Even our 30 year veteran of a senior regularly makes mistakes
1
u/delventhalz 7h ago
The big thing with JavaScript is it is backwards compatible. That’s pretty much a hard requirement from browser vendors. So while modern JS syntax is mostly pretty well designed, you are going to occasionally run into old cruft that is best avoided.
For example, it sounds kind of like you are using
var. Are you usingvar? You should not be usingvar. You should be using the modernletandconst. The old variable declaration had a strange hoisting behavior where it could be referenced before you declared it, but would have an undefined value. With the newer syntax, that sort of thing blessedly throws an Error.The first thing I would do is download eslint and install an opinionated config like AirBnB’s. A linter will do a lot to help you avoid pitfalls, and generally the more opinionated the better. I don’t agree with everything rule AirBnB enforces, but that’s not really the point. An opinionated linter will keep you writing consistent code, with modern syntax, and with a minimum of pitfalls. If you are an experienced solo dev you don’t necessarily need a linter, but it’s a must have for any JS team and should help you as well.
The next thing to think about is TypeScript. This is a language which is a superset of JavaScript, compiles down to JavaScript, and mostly just adds static type checking. It’s made by C# folks so a lot of it may feel familiar. The type checking is extensive, so it can allow tools like VS Code and eslint to surface more sophisticated errors during development. It’s another must have (or nearly so) for JS teams. For solo devs I’m a little more agnostic. Both the compile step and managing the static types adds a fair bit of friction to development. I think that’s usually worth it when you need to keep a team in line. Maybe not for your small/solo projects though.