JavaScript (JS)
Supports event-driven, functional, and imperative programming styles. It has API for working with text, dates, regular expressions, data structures, and the Document Object Model.
Undefined Undefined: A variable has been declared but a value has not yet been assigned to that variable Null: An assignment value to any variable that is meant to contain no value Undeclared: Variables that are not declared or that do not exist in a program
- two ways to declare variables:
let
,const
undefined
means variable is not defined- typeof statement to return type as string
Truthy and Falsy
- Automatic type conversion causes some surprising behaviour
- Surprising results of == equality comparisons, e.g.
- Generally, use
===
for strict equality comparisons
Callback function in JS
Didn’t understand callback function in SimpleKit until I finally Chatgpt it. Apparently, it’s a well known mechanism in JavaScript.
Callback in JS
A callback is a JavaScript function that is passed to another function as an argument or a parameter.
In JavaScript, a callback is a function that is passed as an argument to another function and is executed after the completion of some asynchronous operation or when a certain event occurs. Here’s a breakdown of callbacks:
- Asynchronous Operations: Callbacks are commonly used to handle asynchronous operations, such as fetching data from a server, reading files, or executing timed events like animations. Instead of blocking the execution of code until the operation completes, JavaScript continues executing and invokes the callback function once the operation finishes.
- Event Handling: Callbacks are also used extensively in event-driven programming to handle events triggered by user interactions (e.g., mouse clicks, key presses) or by the system (e.g., timers, network responses). Event listeners are functions that are registered to be called when a specific event occurs.
- Function Arguments: Callback functions are typically passed as arguments to higher-order functions. These higher-order functions accept callbacks and use them to perform tasks once certain conditions are met or operations are completed.
- Error Handling: Callbacks often include error handling mechanisms, where the first argument of the callback function (conventionally named
err
orerror
) is used to propagate any errors that occurred during the execution of the asynchronous operation. This allows for effective error handling and recovery in asynchronous code.
In this example, fetchData
is a function that simulates fetching data asynchronously. It accepts a callback function as an argument and calls it with the fetched data once it’s available. The callback function passed to fetchData
is invoked with the fetched data as its argument when the asynchronous operation completes.
Logical OR and nullish coalescing
- Logical OR
- Assigns a default value when a variable is undefined
- nullish coalescing operator
??
- this is only false when null or undefined
Closures
- When an inner function references the state of an outer function
Factory Functions
- A function that returns a new object
- Can return functions
Lambda Function
- a short and anonymous function that takes one or more parameters and contains a single expression
- usually used to pass a function as a parameter to another function
JSON
Prototype
- All objects have an internal property called the prototype
- The prototype is an object that points to the object it inherits from
- Since the prototype itself is an object, it also has a prototype
- This forms a prototype chain
Super
- Calls the constructor of the parent class
Shadow property
- A property in a subclass that has the same name as a property in the superclass
- The subclass overwrites the superclass
Array
Array methods: