Just JavaScript Course Notes

Personal notes of the Just JavaScript course: Touches foundation concepts of JavaScript in a didactic way. Sometimes it felt like reading a child book, but it is overall a good course that explains the concepts with exercises and even has few historical information to understand them better.

Primitive Values

Primitive values are immutable (cannot be changed).

  • Undefined
  • Null
    • Even though the typeof expression is object. This is because of a historically bug in JavaScript.
  • Boolean
  • Numbers
    • JavaScript uses numbers with limited precision
    • NaN is the result of 0/0 and some other invalid math; stands for “not a number.” and represents an invalid result.
  • Strings
    • They are not objects even though they have properties.
  • BigInts
    • They are great for financial calculations where precision is especially important.
    • However, operations with truly huge numbers may take time and resources
  • Symbols

Expressions

  • “Expressions are questions that JavaScript can answer. JavaScript answers expressions in the only way it knows how—with values.”
  • Expressions always result in a single value.
  • typeof expression returns the actual type of something.
  • literals are expressions that return a primitive value.

Variables

  • Variables are not values.
  • Variables point to values.

Objects and functions

  • “They are also values but, unlike primitive values, I can manipulate them from my code.”
  • The rest outside the primitive values are objects
    • arrays
    • dates
    • and regular expressions
  • By default, they’re mutable.
  • Every time we use the {} object literal, we create a brand new object value.
  • JavaScript doesn’t offer guarantees about when garbage collection happens.
  • Functions are values.
7 is not printed but the function itself.

Equality

  • Same Value Equality: Object.is(a, b)
    • Easy to explain but annoying to write.
  • Strict Equality: a === b or a !== b
    • Used in practice but check the exceptions below.
  • Loose equality (also called “abstract equality”)
    • Many coding standards prohibit the use of == and != in code altogether.

Note: my exercise answer for the task in this topic here.

Equality Exceptions

  • NaN === NaN is false
    • 0 / 0 = NaN
    • However, Object.is(width, height) where width and height are NaN returns true
    • To verify if number is NaN
      • Number.isNaN(size)
      • Object.is(size, NaN)
      • size !== size
  • -0 === 0 and 0 === -0 are true
  • However, Object.is(0,-0)) returns false.

Properties

  • Every expression needs to result in some value, or throw an error.
  • Properties start from objects.
  • You can’t have more than one property with the same name on an object.
  • JavaScript uses a set of rules to get values from properties:
  1. Find out the value of the part before the dot (object.property).
  2. If that value (before the dot) is null or undefined, throw an error.
  3. Else. Check if a property with that name exists on the object:
    a. If it exists, return the value this property points to.
    b. If it doesn’t exist, return undefined.

Mutation

  • Mutation is changing an object’s property. “I have mutated that object” means that one or more properties of an object were changed.
  • Nested objects is not the correct way to think because it makes us forget that there may be other objects pointing to the object we just changed (the problem could arise when we unintentionally modify a value of an object).
  • Solutions:
    • Mutation another object: it is to create another object to point to.
  • No object mutation: Reassign the variable to point to a completely new object.

Let vs Const

  • Some prefer to ban let altogether and always use const. Others say that programmers should be trusted to reassign their own variables.
  • const variables are read-only by definition. However, we can still mutate the object the constant points to:
Here only the shrek variable is read-only. But it points to an object—and that object’s properties can be mutated
  • Remember that const prevents variable reassignment—not object mutation.
  • Be intentional about what you’re mutating and when. The extent to which you’ll rely on mutation depends on your app’s architecture.

Prototypes0

  • Instruct JavaScript to continue searching for a property on another object(s).
  • A prototype is like a relationship.
  • Any JavaScript object may choose another object as a prototype.
  • “JavaScript will search for the property on our object, then on its prototype, then on that object’s prototype, and so on. We would only get undefined if we ran out of prototypes and still hadn’t found our property.”
  • If you want to check if an object has its own property with a certain name which does not look at the prototypes:

Object prototype

  • An object with no prototype.
  • All JavaScript objects get the same prototype by default.
  • We can set a prototype to null:

Prototype pollution

  • Mutating a shared prototype is called prototype pollution.
  • It is recommended to avoid extend JavaScript using prototype pollution but just know this was popular in the past.
  • “You probably won’t use prototypes much directly in practice. However, they are fundamental to JavaScript objects, so it is handy to understand their underlying mechanics. Some advanced JavaScript features, including classes, can be expressed in terms of prototypes.”
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.