You Don’t Know JS

Pt. 1 - Up & Going

Reader Note

I’ve been a Javascript developer for the past year and a half. By reading the “You don’t Know Javascript” series, I’m hoping to hone my vanilla Javascript skills and have a deeper knowledge of the language.

In Up & Going, I’m hoping to review the basics and understand more deeply why the language has the set of tools it has, and perhaps a deeper understanding of why we write Javascript the way we do.

Forward by Jenn Lucas

When you strive to comprehend your code, you create better work and become better at what you do.

Preface

This You Don’t Know JavaScript book series offers a contrary challenge: learn and deeply understand all of JavaScript, even and especially “The Tough Parts”.

Chapter 1: Into Programming

AND logic table

Bit Bit Result
0 0 0
1 0 0
0 1 0
1 1 1

Comments should explain why, not what. They can optionally explain how if that’s particularly confusing.

Warning: For a variety of historical reasons, programming languages almost always count things in a zero-based fashion, meaning starting with 0 instead of 1. If you’re not familiar with that mode of thinking, it can be quite confusing at first. Take some time to practice counting starting with 0 to become more comfortable with it!

do {
  console.log('How may I help you?');
  // Help the customer
  numOfCustomers = numOfCustomers - 1;
} while (numOfCustomer > 0);
function outer() {
    var a = 1;

    function inner() {
        var b = 2;

        // we can access both `a` and `b` here
        console.log( a + b );   // 3
    }

    inner();

    // we can only access `a` here
    console.log( a );           // 1
}

outer();

In the example, you can’t call inner(); on the outermost scope. It can only be called within the outer function scope, as shown.

Chapter 2. Into Javascript

Note: After reading through the first chapter, I realize I don’t really need to review too much. I’m going to skim this chapter and only note the things that I really think are worthwhile. Otherwise, I will keep notes on this chapter to a minimum.

typeof null is an interesting case, because it errantly returns “object”, when you’d expect it to return “null”.

Some simple rules for equality of when to use == or ===.

If either value (aka side) in a comparison could be the true or false value, avoid == and use ===.

If either value in a comparison could be of these specific values (0, “”, or [] – empty array), avoid == and use ===.

In all other cases, you’re safe to use ==. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.

You might think that two arrays with the same contents would be == equal, but they’re not

function foo() {
    var a = 1;

    if (a >= 1) {
        let b = 2;

        while (b < 5) {
            let c = b * 2;
            b++;

            console.log( a + c );
        }
    }
}

foo();
// 5 7 9
(function IIFE(){
    console.log( "Hello!" );
})();
// "Hello!"

The most common usage of closure in Javascript is the module pattern. Modules let you define private implementation details (variables, functions) that are hidden from the outside world, as well as a public API that is accessible from the outside.

Chapter 3: Into YDKJS

This chapter is a preface to the other books. I’ll skip these notes as I’ll be covering this in more detail in those posts.

See you next time!