Functions and Scope

Learn how to write reusable code with functions and scope.

StartJavaScript Illustration
  1. 1

    A function is a block of code designed to perform a task. Functions are like recipes. They take data or variables, perform a set of tasks on them, and then return the result. The beauty of funct...

  2. 2

    How does this code work? [...] 1. On line 1, we have a variable named [...] set to [...] . Our program starts with the calculator in the off position. 2. On line 3, there's a function named ...

  3. 3

    The calculator program should be able to perform a math operation on a number. We should be able to give a calculator a number, have it perform a task on it like multiplication, then print a result...

  4. 4

    If we can set one parameter, can we set two? We can set as many parameters as we'd like by adding them when we declare the function, separated by commas, like this: [...] 1. The [...] functio...

  5. 5

    Using [...] as the result of a function isn't the best use of a function. The purpose of a function is to take some input, perform some task on that input, then return a result. To return a resu...

  6. 6

    In the last exercise, we pointed out that using [...] makes programs more maintainable and flexible, but how exactly? When functions [...] their value, we can use them together and inside one ...

  7. 7

    This unit introduced you to functions. - Functions are written to perform a task. - Functions take data or variables, perform a set of tasks on them, and then return the result. - We can define...

  1. 1

    Scope is a big idea in programming, so let's start at a high level. Scope refers to where in a program a variable can be accessed. The idea is that some variables are unable to be accessed everywh...

  2. 2

    In our theoretical apartment building, you have your own apartment. It has stuff in it that is yours. Other people in the building can't access it. This is like functional scope. You have access ...

  3. 3

    Nice work! Now that we know how global and functional scope contain variables, let's organize some code to practice. To the right, we provided you with incorrect sample code. If you try to run i...

  4. 4

    This unit introduced you to scope. - Scope is the idea in programming that some variables are acessible/inaccessible from other parts of the program. - Global Scope refers to variables that ar...

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Functions and Scope

StartJavaScript Illustration