My 7-Day JavaScript Journey: From Basics to Asynchronous Mastery

rohit mantharohit mantha
6 min read

The past week has been all about diving deep into JavaScript, pushing through challenges, and coming out with a much clearer understanding of the language. As someone who enjoys working with technologies and solving problems, I wanted to share my experiences, the mistakes I made, and the lessons learned during my 7-day JavaScript journey. I hope this can help you avoid some common pitfalls and make your learning process smoother.

Day 1: The Basics - Variables, Loops, and Data Types

I started my journey with the basics of JavaScript: variables, loops, and data types. These are fundamental to any language, but JavaScript has its quirks. The first challenge I encountered was understanding variable scoping. JavaScript has three main ways to declare variables: var, let, and const. I initially mixed up the use of var and let, which led to unexpected bugs because of hoisting.

Mistake #1: Misunderstanding var Hoisting

One of my early mistakes was assuming that variables declared with var behave similarly to those declared with let. In JavaScript, var declarations are hoisted, meaning they are moved to the top of their scope before the code runs, but without their values.

jsCopy codeconsole.log(x); // Output: undefined
var x = 5;

To overcome this, I started using let and const, which are block-scoped and don’t suffer from the same issue.

Key Tip: Always prefer let and const to avoid scoping issues.

Day 2: Functions and Callback Functions

On the second day, I dug deeper into functions, and here’s where things started getting more interesting. Functions are essential in JavaScript, and the concept of callback functions blew my mind. Initially, it was confusing to understand how a function could be passed as a parameter to another function, but it made sense after a few trials.

Mistake #2: Accidentally Invoking a Callback

At first, I made the mistake of writing my callback functions like this:

jsCopy codefunction processUserInput(callback()) { // Wrong!
    const name = prompt('Please enter your name.');
    callback(name);
}

What I didn’t realize is that adding parentheses () executes the function immediately, so it wasn’t being passed as a reference. This caused errors because the callback function was invoked before being passed. Here’s the corrected version:

jsCopy codefunction processUserInput(callback) { // Correct!
    const name = prompt('Please enter your name.');
    callback(name);
}

Lesson Learned: When passing a function as a callback, drop the parentheses. Otherwise, the function will execute before it’s passed.

Day 3: String and Number Manipulation – The Daily Grind

The third day was all about string and number manipulation, which seems simple at first but can lead to surprising challenges. JavaScript has a host of methods to work with strings: .length, .indexOf(), .slice(), .replace(), and more. While most of them are straightforward, I found the behavior of replace() a bit tricky.

Mistake #3: Forgetting that replace() Only Replaces the First Match

I assumed that replace() would replace all occurrences of a substring, but by default, it only replaces the first occurrence.

jsCopy codelet str = 'Hello World, Hello Universe';
str = str.replace('Hello', 'Hi');
console.log(str); // Output: 'Hi World, Hello Universe'

To replace all occurrences, I had to use a regular expression:

jsCopy codestr = str.replace(/Hello/g, 'Hi');

Another challenge I faced was dealing with floating-point precision in numbers. JavaScript's handling of floats, like 0.1 + 0.2, can result in unexpected values, so I had to resort to using parseFloat() and manually rounding results.

Day 4: Arrays and Iteration – For Loops and forEach

On day 4, I tackled arrays and different ways to iterate through them. I initially stuck with basic for loops, but then I discovered how forEach() made my code more concise and readable.

jsCopy codeconst arr = [1, 2, 3, 4];
arr.forEach((element) => {
    console.log(element);
});

However, forEach() doesn’t provide a way to break out of the loop early, which was something I didn’t realize at first. I later learned that if I needed early termination, I should use a regular for loop instead.

Mistake #4: Trying to Break Out of forEach()

jsCopy codearr.forEach((element) => {
    if (element === 3) break; // This will throw an error!
});

Solution: Use for or for...of if you need the ability to stop looping early.

Day 5: Objects and Classes – Structuring Code the Right Way

Day 5 was all about structuring my code with objects and classes. JavaScript objects are incredibly flexible, but with flexibility comes the danger of unintentionally overwriting properties. When working with objects, I learned to always use const to prevent reassignment.

jsCopy codeconst user = {
    name: 'John',
    age: 30
};

I also got comfortable with the class syntax, which simplifies object-oriented programming in JavaScript:

jsCopy codeclass Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    greet() {
        console.log(`Hi, I am ${this.name}.`);
    }
}

While classes are great, one mistake I made was forgetting to use the new keyword when creating an instance of a class, which led to some head-scratching errors.

Day 6: The Asynchronous World of JavaScript

One of the biggest challenges I faced during this journey was understanding asynchronous JavaScript. JavaScript is single-threaded, but through features like promises, async/await, and callbacks, we can perform asynchronous operations (such as API calls or file handling) without blocking the main thread.

Mistake #5: Callback Hell

At first, I used callbacks to handle asynchronous operations, but I quickly found myself in callback hell, where functions were nested too deeply, making my code hard to read and debug.

jsCopy codedoSomething(function(result) {
    doSomethingElse(result, function(newResult) {
        doThirdThing(newResult, function(finalResult) {
            console.log(finalResult);
        });
    });
});

Solution: Promises and async/await

I switched to using promises, which helped me avoid deeply nested callbacks:

jsCopy codedoSomething()
    .then(result => doSomethingElse(result))
    .then(newResult => doThirdThing(newResult))
    .then(finalResult => console.log(finalResult))
    .catch(error => console.error(error));

Finally, I embraced async/await for even cleaner and more readable asynchronous code:

jsCopy codeasync function runTasks() {
    try {
        const result = await doSomething();
        const newResult = await doSomethingElse(result);
        const finalResult = await doThirdThing(newResult);
        console.log(finalResult);
    } catch (error) {
        console.error(error);
    }
}
runTasks();

Day 7: Working with JSON – Parsing and Stringifying

On the last day, I dove into JSON, which is crucial when working with APIs or storing data. The two key methods here are JSON.parse() (to convert a JSON string into an object) and JSON.stringify() (to convert an object into a JSON string).

Mistake #6: Forgetting to Handle JSON Parsing Errors

At first, I didn’t account for potential parsing errors. If the JSON data was malformed, my code would crash. I learned to use try-catch blocks to handle such cases.

jsCopy codetry {
    const data = JSON.parse(invalidJSONString);
} catch (error) {
    console.error('Invalid JSON:', error);
}

Final Thoughts

This 7-day journey taught me a lot more than just the syntax of JavaScript. It exposed me to the common challenges and mistakes developers often face. From callback hell to issues with variable hoisting and JSON parsing errors, each mistake pushed me to find better ways of writing clean and efficient code.

My advice to anyone learning JavaScript is to embrace the mistakes. Each challenge presents a learning opportunity that strengthens your understanding of the language. Keep experimenting, and don’t be afraid to dive into new concepts like async/await early on—they will pay off as your projects grow more complex.

Now, it's your turn to tackle JavaScript. What's the next step in your coding journey?

0
Subscribe to my newsletter

Read articles from rohit mantha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

rohit mantha
rohit mantha