This content originally appeared on DEV Community and was authored by Danish Saleem
JavaScript can be a little daunting to a beginner but it’s important to watch out for mistakes. So, I've rounded up 6 common mistakes beginners make and how you can avoid them, along with a little advice for any aspiring developers/programmers.
Assignment vs equality operators
In JavaScript a single equals sign (=) and double/triple equals sign (==/===) mean very different things so don't get them confused.
// Wrong
const text = "JavaScript";
if ((text = "JS")) {
console.log(text);
} // output = JS
// Right
const text = "JavaScript";
if (text == "JS") {
console.log(text);
} // no output
// OR
if (text === "JS") {
console.log(text);
} // no output
Not using default values
Setting default values for dynamic or optional variables is good practice to prevent unexpected errors due to undefined values.
// Wrong
function addNumbers(a, b) {
console.log(a + b);
}
addNumbers(); // NaN
// Right
function addNumbers(a = 0, b = 0) {
console.log(a + b);
}
addNumbers(); // 0
addNumbers(5); // 5
Addition vs concatenation
A plus symbol (+) is used for both mathematics addition and for combining string. Keep that in mind to avoid any unexpected issues.
// Wrong
console.log(30 + "50"); // 3050
// Right
console.log(30 + 50); // 80
console.log("Java" + "Script"); // JavaScript
Improper naming of variables
It's important to name variables as precisely and accurately as possible. This will prevent accidental duplication and make the code easier to read and understand.
// Wrong
const p = 600;
const d = 100;
const total = 500;
// Right
const price = 600;
const discount = 100;
const totalPrice = 500;
Undefined is not the same as null
Undefined means a variable has not been assigned a value, while null is a special assignment value, which can be assigned to a variable as a representation of no value.
// Wrong
let variable;
console.log(variable); // undefined
// Right
let emptyVariable = null;
console.log(emptyVariable); // null
Misunderstanding scope
A scope can be global or local and refers to the current context of code, which determines the accessibility of variables to JavaScript.
// Wrong
console.log(message); // Error: message is not defined
// Right
// Global scope variable
const firstName = "Dev";
function showMessage() {
// Local scope variable
const message = "Welcome back";
console.log(`${message}, ${firstName}`);
}
showMessage(); // Welcome back, Dev
A little advice
Learning JavaScript (or any programming language) is not all about writing the code or learning the syntax. A big chunk of it is problem-solving and learning the process of identifying and solving the problems you come across. So make sure you don't forget this part and use all the resources available to you to learn how to solve problems. It's what will take you from being a good developer to becoming a great developer!
Let's connect 💜
You can follow me on Twitter, Instagram & GitHub
If you like this post. Kindly support me by Buying Me a Coffee
This content originally appeared on DEV Community and was authored by Danish Saleem
Danish Saleem | Sciencx (2021-11-30T15:33:44+00:00) 6 Beginner JavaScript Mistakes to Avoid!. Retrieved from https://www.scien.cx/2021/11/30/6-beginner-javascript-mistakes-to-avoid/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.