Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. # JS | Functions - Cheat sheet
  2.  
  3.  
  4.  
  5. <br>
  6.  
  7.  
  8.  
  9. ### Function expression
  10.  
  11. Function expression is created when a variable is assigned a function.
  12.  
  13. assigned function can be without name (anonymous) like in the example above, or with name.
  14.  
  15. Function expressions are not hoisted, only the variable is hoisted.
  16.  
  17. ```js
  18. // Function expression
  19.  
  20. var variableName = function ([parameters]) {
  21. // Do something in here
  22. }
  23. ```
  24.  
  25.  
  26.  
  27.  
  28.  
  29. <br>
  30.  
  31.  
  32.  
  33. ### Function Declaration
  34.  
  35. Function declarations are hoisted.
  36.  
  37. ```js
  38. // Function declaration
  39.  
  40. function functionName ([parameters]) {
  41. // Do something in here
  42. }
  43. ```
  44.  
  45.  
  46.  
  47. <br>
  48.  
  49.  
  50.  
  51. ### Arrow function (ES6)
  52.  
  53. Arrow functions are not hoisted, only function declarations are.
  54.  
  55. ```js
  56. // Arrow function ES6
  57.  
  58. var variableName = ([parameters]) => {
  59. // Do something in here
  60. }
  61.  
  62. ```
  63.  
  64.  
  65.  
  66. <br>
  67.  
  68.  
  69.  
  70. ### Concise Arrow function
  71.  
  72. Concise arrow function is used when the code to execute can be written in a single line. Concise arrow function doesn't have `{ }` brackets and we don't need to include keyword `return` (it is implicit).
  73.  
  74. ```js
  75. var variableName = ([parameters]) => /* expresion or statement */ ;
  76. ```
  77.  
  78.  
  79.  
  80. <br>
  81.  
  82.  
  83.  
  84. ### Function `return`
  85.  
  86.  
  87.  
  88. Functions by default return `undefined`.
  89.  
  90. We have to use `return` keyword to return the value from the function and avoid returning `undefined`.
  91.  
  92. The `return` keyword stops the execution, exits the function and returns the result.
  93.  
  94.  
  95.  
  96. ```js
  97. function concatNames(fistName, lastName) {
  98. console.log('before return');
  99. return fistName + ' ' + lastName;
  100.  
  101. console.log('after return'); // This line will not run
  102. }
  103.  
  104. var result = concatNames('John', 'Doe');
  105.  
  106. console.log('result: ' + result);
  107. ```
  108.  
  109.  
  110.  
  111. <br>
  112.  
  113.  
  114.  
  115. ### Callback Functions
  116.  
  117.  
  118.  
  119. In Javascript we can pass functions as parameters to the function.
  120.  
  121. The function that is passed as an argument can be ***called back*** inside of the parent function.
  122.  
  123.  
  124.  
  125. A **higher**-**order function** is a **function** that can take another **function** as an argument, or that returns a **function **as a result.
  126.  
  127.  
  128.  
  129. <br>
  130.  
  131.  
  132.  
  133. **Example:**
  134.  
  135. In the below example `startEatingDinner` is a **Higher order function** simply because it takes another function as argument.
  136.  
  137. `eatDesert` is a function that is being passed as a callback, which is then invoked (called back) by the `startEatingDinner`.
  138.  
  139.  
  140.  
  141. ```js
  142. function eatDessert(){
  143. console.log("Eating the dessert 🍰");
  144. }
  145.  
  146. function startEatingDinner(callback){
  147. console.log("Eating the dinner 🍽");
  148. console.log("Finished eating dinner!");
  149.  
  150. callback();
  151. }
  152.  
  153. startEatingDinner(eatDessert);
  154. ```
  155.  
  156.  
  157.  
  158.  
  159.  
  160. <br>
  161.  
  162.  
  163.  
  164. ### Hoisting
  165.  
  166.  
  167.  
  168. <br>
  169.  
  170. #### Function Declaration hoisting
  171.  
  172. In the below code we call function `sumNumbers` before declaring it, and it doesn't throw an error. Why ?
  173. **Function declarations** are always hoisted to the top of the script by the engine during compilation, before the script runs.
  174.  
  175.  
  176. ```js
  177. sumNumbers(10, 50);
  178.  
  179. function sumNumbers (num1, num2) {
  180. console.log(num1 + num2);
  181. }
  182. ```
  183.  
  184.  
  185.  
  186. <br>
  187.  
  188.  
  189.  
  190. #### ` var` hoisting
  191.  
  192.  
  193.  
  194. As well `var` declarations are hoisted (but not initialized :) ).
  195.  
  196. Meaning that variable is created and hoisted before the script runs, but the value is not assigned.
  197.  
  198.  
  199.  
  200. ```js
  201. console.log(cat); // undefined
  202.  
  203. var cat = 'Marshmallow';
  204.  
  205. console.log(cat); // Marshmallow
  206. ```
  207.  
  208.  
  209.  
  210. **Remember :**
  211.  
  212. `ReferenceError: variable is not defined` is different than `undefined`
  213.  
  214. <br>
  215.  
  216. ## Scope
  217.  
  218. ##### Scope is the context/area in which a variable or afunction is visible from different parts of the program.
  219.  
  220.  
  221.  
  222. In JavaScript there are different types of scope:
  223.  
  224. - Local scope
  225. - Global scope
  226. - Block scope (using `let` and `const`)
  227.  
  228.  
  229.  
  230. ```js
  231. var myColor = "green"; // This is in the global scope
  232.  
  233. function myFunction() {
  234. var firstName = 'John'; // This is in the local/function scope;
  235.  
  236. console.log('local scope - firstName -> ', firstName);
  237. console.log('local scope - myColor -> ', myColor);
  238. }
  239.  
  240. // myColor is accessible as it is a
  241. console.log('Global - myColor -> ' + myColor);
  242.  
  243. // Not accessible because it firstName exists in the local scope of the function
  244. console.log('Global - firstName -> ' + firstName);
  245.  
  246.  
  247. // myFunction has acccess to both of the varabiles;
  248. myFunction();
  249. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement