Skip to content

Instantly share code, notes, and snippets.

@mkmohangb
Last active February 5, 2024 12:56
Show Gist options
  • Save mkmohangb/00a81105b6506f95daa526dff9751a63 to your computer and use it in GitHub Desktop.
Save mkmohangb/00a81105b6506f95daa526dff9751a63 to your computer and use it in GitHub Desktop.

Complete JS course - Udemy

  • console.table
  • debugger (will launch debugger at current line)

Eloquent Javascript by Marijn Haverbeke

3rd Edition

Introduction

  • The art of programming is the skill of controlling complexity.
  • JS introduced in 1995, similar name to Java was inspired by marketing conditions. Standardized as ECMAScript. Latest version ECMAScript 2022. Book uses 2017 version.
  • Some Databases such as MongoDB & CouchDB use JS as their scripting & query language.
  • 3 parts
    1. JS language - basic structure, write more abstract code using objects & functions, error handling, regular expressions, modularity & asynchronous programming
    2. JS in Web browsers - display things on screen, respond to user input and communicate over the network
    3. Node.js
  • There are project chapters in each section.

Chapter 1

  • Numbers - 64 bits

    • 264 - 18 quintillion (18 zeros)
    • 1 bit for sign, some bits to store the position of the decimal point
    • so max. whole number is 9 quadrillion (15 zeros)
    • scientific notation allowed - 2.99e8
    • Operators => +, -, * , /, %
    • Special numbers - +Infinity, -Infinity, NaN
  • Strings

    • single quotes, double quotes or backticks
    • escaping the character using backslash (\n, \t, \)
    • based on Unicode standard using 16 bits per element - characters like emoji take 2 character positions
    • concatenate using + => "con" + "cat"
    • Back quoted strings a.k.a template literals - can span lines, embed other values
      • e.g. `half of 100 is ${100/2}` yields 'half of 100 is 50'
  • Unary operators

    • typeof
    • '-' (minus)
    • !
  • Boolean values

    • true / false
    • console.log(Nan == Nan) => false
  • Logical operators

    • &&, ||, !
    • operator precedence starting with lowest: || , && , comparison (>, ==) and then the rest (chosen this way to have few parentheses in typical expressions)
    • ternary or conditional operator - console.log(true ? 1 : 2) => 1
  • Empty values

    • null & undefined - treat them as mostly interchangeable
  • Automatic type conversion

    • (8 * null) = 0
    • ("5" - 1) = 4 but ("5" + 1) = 51
    • (false == 0) => true
    • if no type conversion should happen during comparison use === or !==. So (false === 0) => false
  • Short-circuiting of logical operators

    • 0, NaN, empty string ("") - count as false among numbers & strings
    • null || "user" => "user", null && "user" => null
    • short-circuit evaluation - true || X => X is not evaluated. Conditional operator works in a similar way - only the one that is selected is evaluated.

Chapter 2

  • Expressions & Statements

    • a fragment of code that produces a value is called an expression. Every value that is written literally is an expression.
    • expression : sentence fragment :: statement : sentence. 1 => expression, 1; => statement
    • program is a list of statements
  • Bindings

    • variable or binding
    • bindings are tentacles which grasp values
    • Use let, const, var to create bindings
    • binding name must not start with a digit, $ and _ are the only special characters allowed.
    • keywords should not be used for binding names.
  • Environment

    • the collection of bindings and their values that exist at a given time is called the environment.
    • a lot of values provided in the default environment have the type function
  • Functions

    • console.log
    • side effect(console.log) vs return value
    • Number, String, Boolean => functions that do type conversion of their arguments
  • Control Flow

    • conditional - if ( 1 + 1 == 2) console.log("wow"). if / else if / else
    • while / do-while (do loop always executes its body at least once)
    • for (let i=0; i< 10; i++) { console.log(i) }
    • break - jumps out of the enclosing loop
    • continue - jumps out of the body and continues with the loop's next iteration
    • switch(expression) { case "rainy": break; case "sunny": break; default: break; } - similar to C/Java
  • Updating bindings succintly

    • ++, --, +=, *=
  • Comments

    • // - single line
    • /* */ - multi-line

Chapter 3

  • A function definition is a regular binding where the value of the binding is a function.

  • const square = function(x) { return x * x; }

  • return without an expression or no return statement at all causes the function to return undefined

  • Bindings & Scopes

    • global & local
    • let & const - local to the block
    • var - throughout the whole function they appear in
    • Each scope can look out into the scope around it
    • when multiple bindings have the same name, code can see only the innermost one.
    • Lexical scoping - Each local scope can see all the local scopes that contain it, and all scopes can see the global scope.
  • Declaration notation - function square(x) { return x * x; }

    • Function declarations are not part of the regular top-to-bottom flow of control so the function can be declared after the code that invokes it.
  • Arrow function

    • const square = x => x * x. When only one parameter name is present, the parentheses around the parameter list can be omitted.
    • Added in 2015.
  • Call stack - Every time a function is called, the current context is stored on the top of the stack.

  • Optional arguments

    • function power(base, exponent=2) {}
    • If you pass too many arguments, the extra ones are ignored. If you pass too few, the missing parameters get assigned the value undefined.
  • Closure

    • A function that references bindings from local scopes around it is called a closure.
    • function multiplier(factor) { return number => number * factor } let twice = multiplier(2); twice(5) // prints 10
  • Recursion - a function that calls itself is called recursive.

  • Pure function - when called with the same arguments, it always produces the same value (and doesn’t do anything else)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment