|
| 1 | +## Functions - Built in |
| 2 | + |
| 3 | +- Function allows us to _group together multiple statements,_ take in some values, perform some operations and return some value. |
| 4 | + |
| 5 | +- Functions take in data known as _arguments._ |
| 6 | + |
| 7 | +- Function may or may not _return_ a value. |
| 8 | + |
| 9 | +- Example: |
| 10 | + |
| 11 | + ```javascript |
| 12 | + Math.max(10, 12); // 12 |
| 13 | + |
| 14 | + ``` |
| 15 | + |
| 16 | + The above line is a JavaScript statement. |
| 17 | + |
| 18 | + 10 and 12 passed to the function are arguments, separated by comma. |
| 19 | + |
| 20 | + 12 is _returned_ from the function. |
| 21 | + |
| 22 | +- There are many in-built JavaScript functions. |
| 23 | + |
| 24 | + e.g: |
| 25 | + |
| 26 | + - `console.log('hey');` returns `undefined` , logs `hey`. |
| 27 | + - `parseFloat('2.032565') // 2.032565` (converts string to number) |
| 28 | + - `parseInt('2.032565') // 2` (converts string to number as integer) |
| 29 | + - Many date functions are also present. e.g. `Date.now()` returns no. of milliseconds since January 1, 1970 00:00:00 UTC. |
| 30 | + - DOM functions: |
| 31 | + - Example: |
| 32 | + |
| 33 | + ```html |
| 34 | + <body> |
| 35 | + <p>Hey How ya doin?</p> |
| 36 | + <script> |
| 37 | + const para = document.querySelector('p'); // finds p tag in page |
| 38 | + console.log(para); // <p>Hey How ya doin?</p> |
| 39 | + </script> |
| 40 | + </body> |
| 41 | + |
| 42 | + ``` |
| 43 | + |
| 44 | + - Mobile only functions e.g. `navigator.vibrate()` |
| 45 | + |
| 46 | +- In case of doubts, always refer MDN Docs. |
| 47 | + |
| 48 | +- Other Examples: |
| 49 | + |
| 50 | + ```javascript |
| 51 | + scrollTo(0, 200); // scrolls to (x, y) position in page |
| 52 | + |
| 53 | + scrollTo({ |
| 54 | + top: 500, |
| 55 | + left: 0, |
| 56 | + behavior: 'smooth' |
| 57 | + }); // scrolls to position top: 500, left: 0 in a 'smooth' manner |
| 58 | + |
| 59 | + ``` |
| 60 | + |
| 61 | + The `scrollTo` function returns `undefined`. |
0 commit comments