Fireship · @fireship

JavaScript Function - What's your Function?

Uploaded August 6, 2019 · Length 12:27

JavaScript Function - What's your Function?

Summary

JavaScript functions explained, including declarations, expressions, parameters, and arrow syntax.

  • javascript functions
  • function declarations
  • function expressions
  • parameters
  • arguments
  • arrow functions

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    Grossberg it's great to learn because

  2. 0:04

    knowledge is power functions are the

  3. 0:06

    backbone of JavaScript development yet

  4. 0:08

    also one of the most difficult concepts

  5. 0:09

    to master but once you understand

  6. 0:11

    functions everything else in the

  7. 0:12

    language becomes relatively easy we'll

  8. 0:14

    start from the absolute basics then go

  9. 0:16

    into practical examples of arrow

  10. 0:17

    functions closures higher-order

  11. 0:18

    functions recursion and we'll even talk

  12. 0:21

    about some common best practices for

  13. 0:22

    writing high-quality JavaScript code

  14. 0:24

    welcome to episode 4 of the full course

  15. 0:26

    JavaScript function if you're new here

  16. 0:30

    like and subscribe and you can follow

  17. 0:31

    the full free course on fire ship IO now

  18. 0:34

    in order to understand a function let's

  19. 0:36

    start by dissecting it and looking at

  20. 0:37

    its anatomy what we're looking at here

  21. 0:44

    is a function declaration also called a

  22. 0:46

    function definition or statement it

  23. 0:47

    starts with the function keyword then

  24. 0:49

    you need to choose a name for it and you

  25. 0:51

    should choose something that's readable

  26. 0:52

    to other human beings because there's a

  27. 0:54

    good saying there's only two hard things

  28. 0:55

    in computer science cache and validation

  29. 0:57

    naming things and off-by-one errors in

  30. 1:00

    other words choosing the right name for

  31. 1:01

    your function is almost as hard as

  32. 1:03

    making a joke about programming but once

  33. 1:05

    you decide on a name the next step is to

  34. 1:07

    add parentheses and then add any

  35. 1:08

    parameters or inputs to the function you

  36. 1:11

    can think of a parameter as a variable

  37. 1:13

    that this function lot access to when

  38. 1:15

    it's called and then from there we have

  39. 1:16

    curly braces and everything inside is

  40. 1:18

    the function body and that's where you

  41. 1:20

    define all your logic the function body

  42. 1:22

    will typically do one of two things

  43. 1:24

    it will either perform a task or return

  44. 1:25

    a value or possibly both an example of a

  45. 1:28

    function performing a task is a console

  46. 1:30

    log and because there's no return

  47. 1:32

    statement it will implicitly return

  48. 1:34

    undefined but a function body might also

  49. 1:36

    contain a return statement and that

  50. 1:37

    allows us to take the value that was

  51. 1:39

    calculated inside the function body and

  52. 1:41

    set it as a variable now at this point

  53. 1:43

    we've declared a function which

  54. 1:44

    describes what the code will do but in

  55. 1:46

    order to actually execute it we need to

  56. 1:48

    call the function we do that by

  57. 1:49

    referencing the function by its name and

  58. 1:51

    then we pass in actual values as

  59. 1:53

    arguments to the parameters so imagine

  60. 1:55

    you get asked on an interview what is

  61. 1:57

    the difference between a parameter and

  62. 1:59

    an argument you might say something like