📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • Conceptual Aside: Types and JavaScript
  • Primitive Types
  • Conceptual Aside: Operators
  • Operator Precedence and Associativity
  • Conceptual Aside: Coercion
  • Comparison Operators
  • Strict equality operator
  • Existence and Booleans
  • Default Values
  • Framework Aside: Default Values
  • Solution
  1. Javascript
  2. Js Weird Parts

Types And Operators

Conceptual Aside: Types and JavaScript

Dynamic typing is the idea that the JavaScript engine figures out what type of data that a variable holds while your code is running.

This allows you to change the data type as the code is running, and the engine will figure out the data type automatically.

(Contrast this to static typing, where you tell the compiler the data type you intend to hold inside a variable ahead of time.)

Primitive Types

Primitive types are data types that represent a single value, i.e., is not an object or collection of name/value pairs.

  1. undefined: stands for lack of existence (set by JS engine when variable isn't assigned)

  2. null: also stands for lack of existence, but it's something you can set!

  3. Boolean

  4. Number: it's a floating point number, so it always has decimals! (This can make math weird.)

  5. String

  6. Symbol

Conceptual Aside: Operators

Operators are actually syntactic sugar for functions that take 2 arguments and return 1 result.

Operators are functions with infix notation (operator is between the 2 values).

Operator Precedence and Associativity

Operator precedence is about which operator function gets called first. Higher precedence always wins.

Operator associativity refers to the order that operator functions get called when they have the same precedence. This can be left-to-right or right-to-left.

Example of precedence:

var a = 3 + 4 * 5;
console.log(a);

* has a higher precedence than +, so * operator function gets run first.

Example of associativity:

var a = 2, b = 3, c = 4;

a = b = c;

Every variable will be 4 because the assignment operator has right-to-left associativity!

Pro tip: Remember how assigning a variable in the browser console returns the value assigned? That's because it's a function taking 2 values and returning a result! In the example above, b = c returns 4, which evaluates to a = 4!

Conceptual Aside: Coercion

Coercion is converting a value from one type to another. This happens in JavaScript often because it's dynamically typed.

var a = 1 + "2";

The JavaScript engine coerces 1 into a string because it's trying to dynamically type the data for you in order to complete the operation.

Comparison Operators

Everything we learnt so far comes together here.

console.log(1 < 2 < 3); // returns true
console.log(3 < 2 < 1): // also returns true

The first line above makes sense, but the second line seems wrong. What's going on with the second line?

Recall that the > operator above is used across the board, so we're dealing with an associativity problem.

> operator has a left-to-right associativity. Therefore, 3 < 2 evaluates to false.

false < 1 then leads to coercion. When you coerce false into a number, it becomes 0. And we know that 0 < 1 is true!

Pro tip: To check what a value coerces into when pushed into another data type, use the class constructor for that data type. For example, Number(false) will coerce false into 0.

Note: Even 1 < 2 < 3 is not evaluating to true for the reasons you expect. It works only because of coercion as well.

Strict equality operator

The equality operator only checks for equality of value (and not type) because it utilizes coercion!

So "3" == 3 will coerce 3 into string, which is why it returns true.

On the other hand, the strict equality operator prevents coercion, which is why it only evaluates to true when the values compared are of the same value and type.

Pro tip: The equality operator is weird in that it doesn't behave as expected. For example, null == 0 returns false even though null can be coerced into 0. That's why it's best practice to use the strict equality operator to avoid these issues.

Existence and Booleans

If you've ever used existence in an if statement, that works because of coercion as well!

var a = "I exist!";

if (a) {
  // run code
};

a gets coerced into a boolean!

Pro tip: Be careful with the number 0, as it coerces to false, but you may not always want it to be treated as false.

Default Values

Unlike many programming languages, JavaScript doesn't care if you fail to provide an argument for a function.

That's because when a function is invoked, it creates an execution context that automatically places the parameter into memory space as undefined.

function greet(name) {
  console.log("Hello " + name);
};

greet(); // logs "Hello undefined"

What if you want to set a default value for parameters like name? Here's a neat trick:

function greet(name) {
  name = name || "Default name";
  console.log("Hello " + name);
};

The or || operator has a special behaviour where if it has two values that can be coerced to a boolean, it will return the first value that can be coerced to true.

If name is undefined, it will return "Default name" because that's the only value that can be coerced to true. If name has a value, it will return that value instead!

Similar cases: 0 || 1 will return 1. "Hello" || "" will return "Hello".

Framework Aside: Default Values

The or || operator trick is used by libraries often.

Suppose you have 3 JS files that you import into your index.html.

<script src="lib1.js">
<script src="lib2.js">
<script src="app.js">

Suppose that these JS files have the following lines of code:

// In lib1.js
var libraryName = "Lib 1";

// In lib2.js
var libraryName = "Lib 2";

// In app.js
console.log(libraryName);

When you load multiple JS files, it's all under one execution context. Each file just stacks one on top of the other.

That means that libraryName will console log "Lib 2". This is a problem for frameworks/libraries because you don't want to collide with other code.

Solution

Frameworks/libraries will do this to avoid collision of other code:

window.libraryName = window.libraryName || "Lib 2";

Essentially, many frameworks/libraries will first check to see if the variable name for the library is already taken in the global object. If it's not, "Lib 2" will be stored in the variable.

Often times, "Lib 2" is basically the entire framework/library in a namespace!

So, if a conflict occurs, the framework/library just won't load, which is helpful for debugging!

PreviousExecution Contexts And Lexical EnvironmentsNextObjects And Functions

Last updated 3 years ago