Javascript: Mistakes per day
Tags: javascript, webdev, programming, discuss
Published: 2022-02-12 04:24:20

`Beginner:` We are humans and mistakes are very much common. > 50 mistakes/day.

`Intermediate:` We are humans and we can avoid mistakes. < 50 mistakes/day.

`Expert:` We are humans and we can speak in Javascript 🤣. < 5 mistakes/day.

Quite interesting, isn't it? If you see in the above statements mistakes are there everywhere. So, what an expert mind will do generally? 

He can forecast the result and accordingly update the result whereas a beginner does the trial and error while coding. 

> If you know the disease well then you can cure it well instead of eating the whole medical shop.

There could be a good proverb in Google but suddenly got this in my mind so written it. Hope you got the context.

I don't like to beat around the bush. Let's start:

I want to provide some cool tips to avoid mistakes and to code in less time.

### Tip 1: Practice basics well

Whenever we code anything we decouple the whole feature into small parts as tiny as possible and will start coding.

In each phase of the coding, we somehow play with data that we want to show on the browser.

**Example:**

Let's say you have to show the employee list in your application and update the list as soon as the user adds a new employee.

```
// Initial list.
let employeeList = ['james', 'peter', 'roy', 'garreth'];

// you know you can use push and add a new employee to your existing array.
employeeList = employeeList.push('new james');

// But you are not sure what happens if you do like the above
console.log(employeeList);
> 5 // push returns the size of the array instead updated array.
```
So, knowing the basics is very much important to avoid mistakes. Also, to avoid trial and error trap.

>> I would say this is the main reason for mistakes in the whole coding journey.

### Tip 2: Learn before using

Yes. You don't use basic JS methods every time as you might have to depend on wrappers or libraries which are written to achieve a few specific real-time scenarios in web applications.

**Example:** `moment.js` - it is a javascript library used to deal with date and time very well.

We can directly import this library and we do the necessary time conversions etc. 

Now, these kinds of libraries provide different methods to use for different purposes. We shouldn't search StackOverflow and copy-paste the code.

Every library provides cleaner documentation for each feature they provide. Just go through them and try examples they provide and understand features  & shortcomings. After that use them.

> So, this tip is not just to avoid mistakes but also to avoid future bugs.

### Tip 3: Understand the flow of the execution

Executions generally happen from top to bottom in the script. Though, there are situations where flow slips a little bit.

**Aynschronous JS:**

JS has asynchronous support like Promises, setTimeout, setInterval etc. Execution flow changes if we use these like:

**Example:**

```
console.log('first message');

setTimeout(() => {
    console.log('async message');
});

console.log('last message');

> first message
> last message
> async message
```

**Operator Precedence:** It determines how operators are parsed concerning each other.

```
console.log(5 + 10 * 3 - 2); // 33
console.log(5 + 10 * (3 - 2)); // 15
console.log((5 + 10) * 3 - 2); // 43
```
In first scenario, multiplication has higher precedence so,

`5 + 10 * 3 - 2 -> 5 + 30 - 2 -> 35 - 2` - 33

In second scenario, parenthesis has higher precedence so,

`5 + 10 * (3 - 2) -> 5 + 10 * 1 -> 5 + 10 -> 15`

The third is self-explanatory.

So, the order of execution changes results here. 

So far good but there are many rules in operator precedence how can we remember?

Don't remember just bookmark this page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

> Keep in mind that debugging is not for mistakes.

I will cover debugging in another article where I will point where, when, and how to use debugging efficiently.

What are the takeaways?

1. Fewer mistakes == less time to code - Performance improvement
2. To avoid mistakes you will learn  - Knowledge is divine
3. You'll end up speaking in JS - Became the finest programmer 🤩

All these tips out my experience. Please do comment on which tip you follow the most and also comment your own tips.

Thank you 😊

<hr style="border-width: 2px;border-color: #87CEEB;">
<h1> 💎 Love to see your response</h1>
<ol>
<li style="padding: 10px;font-size: 30px;"><b>Like</b> - You reached here means. I think, I deserve a like.  </li>
<li style="padding: 10px;font-size: 30px;"><b>Comment</b> - We can learn together. </li>
<li style="padding: 10px;font-size: 30px;"><b>Share</b> - Makes others also find this resource useful.</li>
<li style="padding: 10px;font-size: 30px;"><b>Subscribe / Follow</b> - to stay up to date with my daily articles. </li>
<li style="padding: 10px;font-size: 30px;"><b>Encourage me</b> - <a href="https://www.buymeacoffee.com/urstrulyvishwak" target="_blank">You can buy me a Coffee</a>
</ol>
<hr style="border-width: 2px;border-color: #87CEEB;">

<h2> Let's discuss further. </h2>

<ol style="padding: 20px;">
<li style="padding: 10px;font-size: 30px;">Just DM <br> <a href="https://twitter.com/messages/compose?recipient_id=1388837509824610314&text=Hello world"
class="twitter-dm-button" data-screen-name=“@urstrulyvishwak” data-size="large">@urstrulyvishwak</a>
</li>
<li style="padding: 10px;font-size: 30px;">
<p> Or mention <br> <a href="https://twitter.com/intent/tweet?screen_name=urstrulyvishwak&ref_src=twsrc^tfw" class="twitter-mention-button" data-size="large" data-text="Hi, Vishwak" data-related="@urstrulyvishwak" data-show-count="false">@urstrulyvishwak</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script></p> </li>
</ol>

<h2> For further updates: </h2>
<a href="https://twitter.com/urstrulyvishwak?ref_src=twsrc^tfw" class="twitter-follow-button" data-size="large" data-text="Hi, Vishwak" data-related="@urstrulyvishwak" data-show-count="false">Follow @urstrulyvishwak</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>