They said I don't know JS - 3

They said I don't know JS - 3

Here I sat, thinking, "Is it worth it writing this blog ?". Then I figured that this was the only I can get my thoughts out by writing the third part of lessons learned from You Don't Know Javascript.

Anyway Hi โœ‹๐Ÿผ,

Welcome back to a new section.

I was wondering what else I could discuss and I went along with talking about some other concepts and explanations discussed in the book. As always let's break it down into bits

  • Arrays

    A Javascript array is an object capable of holding any indexed value of any type.

    But the interesting thing about JS arrays is that it is an extendable , referencable object, you can configure them to your taste e.g

      let idxs = []
    
      // extending the array 
      idxs.__proto__.sum = function(){
        return this.reduce(function(accumulator, currentValue) {
            return accumulator + currentValue;
        },0);
      }
    
      function fill(n){
          for (var i = 0 ; i < n; i++){
              idxs.push(i) // fill with values
          }
      }
    
      fill(10) // idxs : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
      console.log(idxs.sum()) // 45
    

    Try to imagine how to apply such functionality in your code. An example can include you adding a average method to a custom array.

  • Functions as an object

    "Functions in JavaScript are more than just blocks of code. They are special objects that can be named, referenced, and invoked to perform a specific task."

    The above definition was referenced in my post on LinkedIn. In the definition, it emphasizes the term object. for example in the code below

function foo(){
    console.log(foo.time) // produces a date every interval
}

foo.__proto__.time = new Date() // shows foo function having an object property

setInterval(foo, 1000) // setInterval invokes foo which indicate foo function attribute.
  • Equality and inequality

    Coercion sentiment in javascript is a very popular discussion, but YDKJS explained it has been learnable so let's take a bit of time to understand it.

    Coercion comes in two forms javascript implicit and explicit. Explicit is noticeable in code when the conversion from one type to another while Impicit cohesion happens in a very subtle way as a side effect of some other operation e.g addition

      let a = 42;
      let b = String(a) // "42" Explicit
    
      let c = "56"
      let d = c * 2 // 112 Implicit
    

    In the practical sense, implicit cohesion happens in javascript as a result of trying to assert the nature of "truthiness" or "falseness" of variables or conditions.

    The specific list of falsy values in JS is:

    • ""- empty list

    • 0 ,-0 , NaN - invalid number

    • null,undefined

    • false

      Any value that's not in the list above is truthy.

      With this, we can talk about how knowledge cohesion help in equality and inequality checks in JS using the code below, particularly with the == , ===,!= and !== :

        let w = "34"
        let v = 34
      
        w == v  // true
        w === v // false
      

      In == types are not matched so there is an implicit cohesion to assert equality but in === The types of variables are been matched along the values without cohesion.

      So the simple rule of coercion is :

      • if either value in comparison could be true or false then use === .

      • if either value is in comparison could be 0,"",[] use === .

      • in all other cases use ==, since it simplifies the code.

Essentially these are the key takeaways for this third part of the discussion of this series.

Please share your thought about the article and if you find it interesting you can follow me on LinkedIn here