AJAX-(Asynchronous, JavaScript, and XML)
AJAX is a an approach
Functions
Function expression
functionOne = function() {//some code};
Function declaration
function functionTwo() {//some code};
Null vs Undefined
let x = null;let y = undefined;//thenx == y// return true;
Ref: Abstract equality comparison
https://www.ecma-international.org/ecma-262/6.0/#sec-abstract-equality-comparison
Callback functions
a function that is passed into another function as a parameter then invoked by another function.
Higher order function
a function that accepts a callback as a parameter
EXAMPLE
function callback(){console.log("Coming from callback");}function higherOrder(fn){console.log("About to call callback");fn(); // Callback function is invokedconsole.log("Callback has been invoked");}higherOrder(callback);
Use Cases
- Advanced array methods
- browser Events
- AJAX requests
- React development
For Each function
Simple Example
var arr = [1,2,3,4,5,6];forEach(arr, function(number){console.log(number*2);});
Generalized
function forEach(array, callback){// To be implemented}// Callback signaturefunction callback(curElement, currentIndex, array){// Implemented by the caller of forEach}
EXAMPLE
var strings = ["my", "forEach", "example"];var result = "";forEach(strings, function(str, index, array){if (array.length - 1 !== index){result += str + " ";} else {result += str + "!!!";}});
forEach logic EXAMPLE
function forEach(arr, callback){for(let i = 0; i < arr.length; i++){callback(arr[i], i, arr);}}
findIndex
Returns the index of the first element in the array for which the callback returns a truthy value. -1 is returned if the callback never returns a truthy value.
function findIndex(array, callback){// findIndex code to be implemented}function callback(curElement, curIndex, array){// callback implemented by caller of function}
findIndex logic Example
function findIndex(arr, callback){for(let i = 0; i < arr.length; i++){if(callback(arr[i], i, arr)){return i;}}return -1;}
Styling and Code Quality
To Check out
Resources and Reference
Notes within this doc have been drawn from the following sources: