javascript
javascript
- use MDN as your reference
- object-oriented. No distinction between types of objects. Inheritance is through the prototype mechanism, and properties and methods can be added to any object dynamically.
- javascript cannot automatically write to disk
- it is standardized at ECMA international - the standardized version is called ECMAScript and companies can use the open spec to develop their own implementations.
- the ECMAScript specification does not describe the Document Object Model (DOM).
- many JS engines exists - spidermonkey by firefox, v8 by chrome.
- there are some pretty interesting web APIs available - https://developer.mozilla.org/en-US/docs/Web/API
- var - declares a variable, optionally initializing it to a value
- let - declares a block-scoped, local variable, optionally initializing it to a value
- const - declares a block-scoped, read-only named constant
- you can declare variables to unpack values using the destructuring assignment syntax
- undeclared variables lead to ReferenceError
- declared but unassigned variables get the value, undefined which is a primitive value in this language
- typeof undefined returns undefined
- const declarations always need an initializer, else lead to SyntaxError
- scope types - global, module, function, block
- var-declared variables are hoisted, meaning you can refer to the variable anywhere in its scope, even if its declaration isn't reached yet. You can see var declarations as being "lifted" to the top of its function or global scope. however, if you access a variable before it's declared, the value is always undefined, because only its declaration is hoisted, but not its initialization.
- a variable declared with let, const, or class is said to be in a "temporal dead zone" (TDZ) from the start of the block until code execution reaches the place where the variable is declared and initialized. while inside the TDZ, the variable has not been initialized with a value, and any attempt to access it will result in a ReferenceError.
- unlike var declarations, which only hoist the declaration but not its value, function declarations are hoisted entirely — you can safely call the function anywhere in its scope.
- data types - Number, BigInt, String, Boolean, Symbol, undefined, null
- typeof null is object
- comparison with null and undefined is pretty non-intuitive
- null == undefined returns true
- null === undefined returns false
- loop - basic for, do..while, while, for..in, for…of
- all objects have a special property [[Prototype]], that is either null or references another object.
- if you assign obj1 to obj2’s proto value, obj2 will have all properties of obj1. obj2 has basically inherited obj1.
- proto is a historical getter/setter for [[Prototype]]
- since it is a little outdated, it is suggested to use Object.getPrototypeOf/Object.setPrototypeOf
- no matter where the method is found: in an object or its prototype. In a method call, this is always the object before the dot
- Object.keys only returns own keys but for…in returns both own and inherited keys
- obj.hasOwnProperty for checking if property is own
- new objects can be created with a constructor function, like new F()
- every function has the "prototype" property even if we don’t supply it.
- Object has a default toString method - returns “[object Object]”
- read up on extending errors, can be pretty helpful
- closures - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
- event based programming in the browser is a pretty good example of closures
- this is fucking weird - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
- sorting of array happens in-place
- to sort the elements in an array without mutating the original array, use toSorted()
- typed arrays are made up of Buffers and Views - useful stuff but read about them when you need them
- garbage collection algorithms - reference counting (not used by any existing engine implementations), mark-and-sweep
- built-in eval function allows to execute a string of code
- javascript is single threaded and synchronous
- at low-level, all info is stored as key-value pairs in what we call as variable environment
- at low-level, all code is executed in thread of execution
- thread of execution + variable environment = execution context
- new function invoke => new execution context created
- call stack maintains the order of execution of execution contexts
- hoisting: variable declarations are scanned and made undefined, function declarations are scanned and made available
- global space: anything that is not in a function, is in global space
- variables present in global space can be accessed by window object
- in global space, window === this
- undefined is like a placeholder till a variable is assigned some value
- undefined does not mean not defined
- javascript is loosely typed or weakly typed, types have flexibliity
- not a good practice to explicitly assign undefined to a variable
- let and const are hoisted but allocated memory in place different than the window object, cannot be accessed until initialized, hence lie in temporal dead zone
- prefer const over let over var
- a block is defined by {}
- let and const are block-scoped
- shadowing: giving a variable in a child scope the name of a variable in the outer scope
- closure: function + its lexical environment
- use of closure: module design pattern, currying, once, memoize, maintain state in async, setTimeout, iterator
- function statement/declaration: normally defined function fun() {}
- function expresssion: created and assigned to a variable
- anonymous function: function without a name
- named function expression: function with a name, assigned to a variable, but not accessible in global scope with that function name, only by the variable name
- functions in javascript are first-class: used as values, can be passed as arguments, can be executed inside a closure function, can be returned
- callback function: function passed as argument to another function
- things which take time can block the main thread
- event loop = call stack + microtask queue + callback queue
- microtask queue: promises, mutation observers
- microtask queue > callback queue: this can lead to starvation of functions in callback queue
- higher-order function: take another function as argument or return a function
- be comfortable with map, filter, and reduce
- promise: do some work, then resolve it or reject it. user of your promise has to write callbacks for then, catch, finally
- promise states: pending, fulfilled, rejected
- settled state = fulfilled or rejected
- promise API: all, allSettled, race, any, resolve, value
- async/await: wait for a promise to settle
- nodejs talk by ryan dahl - https://www.youtube.com/watch?v=EeYvFl7li9E&ab_channel=JSConf
- concurrent request handling - https://stackoverflow.com/questions/34855352/how-in-general-does-node-js-handle-10-000-concurrent-requests
- the learn section - https://nodejs.org/en/learn/
- when is it single threaded - https://www.youtube.com/watch?v=gMtchRodC2I&ab_channel=HusseinNasser
- libuv - https://www.youtube.com/watch?v=c51fcXRLGw&abchannel=node.js
- you don’t know node - https://www.youtube.com/watch?v=NLtL-EEclRc&ab_channel=JSConf
- profiling - https://www.youtube.com/watch?v=ASv8188AkVk&ab_channel=InfoQ
<- back to home