Skip to content

Instantly share code, notes, and snippets.

@Eronmmer
Created February 24, 2020 09:36
Show Gist options
  • Save Eronmmer/b5c5839e23c440e828104d045ae517a6 to your computer and use it in GitHub Desktop.
Save Eronmmer/b5c5839e23c440e828104d045ae517a6 to your computer and use it in GitHub Desktop.
Random JavaScript musing inspired by the YDKJS book by Kyle Simpson

Random JavaScript musings inspired by the YDKJSY book 🔥

  • Values come in two forms in JavaScript. primitive and object. Primitives include: strings, boolean, numbers(another variation to this is bigint: big-integer which is used for storing arbitrarily large numbers), null, undefined and symbol. Object values are: Arrays and Objects. It's important to note that functions in JS are a special type of object.

  • The best semantic use of const is when you have a simple primitive value that you want to to give a useful name to, such as using myBirthday instead of true. This makes programs easier to read.

  • Other ways variables can be declared are in function parameters and in catch clauses. The former behave var-ish while the latter behaves const and let-ish

  • Function declarations are so named because they appear as statements by themselves. Not as expressions in another statement. For this reason, they can be hoisted in contrast to function expressions which are usually defined and assigned to variable names. It is important to note that a function expression is not associated with it's identifier until during runtime. Not all PLs treat functions as values as JS does. This is very crucial for a language to support the FP pattern as JS does. The fact that functions are objects is another reason why they can be assigned as properties on objects(usually known as methods).

  • In JS, we must be aware of the nuanced differences between an equality comparison and an equivalence comparison.

  • The === operator isn't what we think it is in every situation. Infact, it is designed to lie in two cases of special values: NaN and -0. Consider:

NaN === NaN    //false
0 === -0       //true

So, the truth is that === is not actually strict exactly equal comparison in the strictest sense

  • When using === on objects, it doesn't just check for equality in values and types. It also checks for reference-copy.

  • Most people feel that == only checks for values and not types. The truth is that both == and === check for values and types. The only difference between the two is that == allows coercion before the comparison. So, == and === both check for values and types but == allows type conversions(coercions) first and once the types have been converted to be the same on both sides, then == does the same thing as ===. Instead of describing == as being a loose equality operator, it should be described and seen as a coercive equality operator.

  • When using relational operators on strings and numbers, coercion usually takes place except in the case where both values being compared are already strings; in this case, they use alphabetical (dictionary-like) comparison of the strings. For example;

const x = "10";
const y = "9";

x < y;      // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment