Beginner’s Mistakes: JavaScript’s Variables, Functions & Objects. . . Oh My!
JavaScript post-class four of coding bootcamp, I find my head a bit ‘dazed and confused’. . . a far cry from HTML’s elements, CSS’s selectors and properties, and, yes, even CSS grid. Instead, variables, functions, objects, properties, methods, events, conditions and loops are swimming around in my brain. So, I do what any normal noob would do, look for misery. . . I mean. . . company, googling “beginner common mistakes” in JavaScript . . . I find myself. Sigh. OK. Common mistakes are universal, save the random coding savant.
Among the most cited common mistakes, syntax issues seem to rank the highest. This includes: missing or mismatched brackets, parentheses, curly brackets, commas and semicolons; failure to camel case when naming variables; and naming variables with JavaScript reserved words . . . to name a few.
Another favorite of those new to JavaScript, is confusing assignments and comparisons. I can personally attest to this one. There is a difference between assignment “=” and comparison “==” (forgiving equality) and “===” (strict equality), and the difference is functioning code vs. a non-functioning code.
If you can relate to the mistakes that I have described, worry no more! I have provided some examples and charts below to assist in understanding how variables, functions and objects work at a fundamental level with some syntax highlights. Hope it helps! Keep calm and code on.
Top comments (4)
For you endless journey learning JS, I'd recommend you to bookmark devdocs.io/javascript/. I've been developing with javascript for almost ten years now and I check these docs almost every day.
Thanks! Appreciate you.
Hi Tisha,
That's a pretty good start to understanding some of the complexities if JavaScript.
There are a couple things I'm not certain of your meaning. When I look at the array syntax you showed, it shows a name outside the list. That's not a format I've ever seen for arrays.
As for functions, you've shown one way of doing them, and as an experienced JavaScript developer, it's most definitely not my favorite trend, because it adds confusion to how and when functions can be used and may force functions to be ordered in a confusing and disorderly way, depending on how code is done.
Functions can be written this way
function multiply(num1, num2) {
return num1 * num2;
}
When you use the form where you assign an anonymous function to a constant, the function does not get named and can be harder to find in call stacks. Additionally, they can't be called until after they are assigned, which means if you have any code that is inline that is using that function, it must not be written until after the function declaration. This can really start cluttering up code.
JavaScript can seem a bit daunting at first. It's actually probably too easy to learn the fundamentals of JS. If I can offer a couple pieces of advice that will truly help beginners get the most out of JavaScript it's this. Focusing on writing, clean, organized, easy to read and understand code is top priority in most JavaScript based apps. One practice that can help most people do this with very little effort is to get into the habit of test first development (red, green, refactor). Write a unit test that tests a functional requirement. Make sure it fails (red). Make it pass with the most direct amount of coding needed (green). Clean up the code that made it work and make sure all tests continue to pass (refactor), then repeat for the next requirement. This practice is not only easier in JavaScript than it is in any other language, it is also more important in JavaScript than any other language. That's because JavaScript is extremely flexible and will let you write horrible code just as easily as good code and it will still work, but it will cost you dearly, later. TFD helps enforce good habits that JavaScript does not enforce itself.
Good luck, I wish you the best. JavaScript is truly a powerful, flexible language and mastery of it can be extremely useful.
Chris,
Thank you for taking the time to read the blog and provide input! Very much appreciated. This is a very supportive community. I look forward to the journey.