6 Beginner JavaScript Mistakes to Avoid!
Tags: javascript, codenewbie
Published: 2021-11-30 15:33:44

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.

```JavaScript
// Wrong

const text = "JavaScript";

if ((text = "JS")) {
  console.log(text);
} // output = JS
```

```JavaScript
// 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.

```JavaScript
// Wrong

function addNumbers(a, b) {
  console.log(a + b);
}

addNumbers(); // NaN
```

```JavaScript
// 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.

```JavaScript
// Wrong

console.log(30 + "50"); // 3050
```

```JavaScript
// 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.

```JavaScript
// Wrong

const p = 600;

const d = 100;

const total = 500;
```

```JavaScript
// 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.

```JavaScript
// Wrong

let variable;

console.log(variable); // undefined
```

```JavaScript
// 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.

```JavaScript
// Wrong

console.log(message); // Error: message is not defined
```

```JavaScript
// 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](https://twitter.com/MrDanishSaleem), [Instagram](https://www.instagram.com/mrdanishsaleem/) & [GitHub](https://github.com/mrdanishsaleem/)

If you like this post. Kindly support me by [Buying Me a Coffee](https://www.buymeacoffee.com/mrdanishsaleem)

![Buy Me a Coffee](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fefzbxbnwww7tkght33w.png)