JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • Key Notes
  • File Location
  • Sample Code
  • Return
  • Calling the Functions
  • Hoisting
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 4-Functions

Declarations

Function declarations, otherwise known as function statements, are one of the ways to create a function in JavaScript. You'll notice them when you see the function keyword starting them off. Let's look at a few key rules.

Key Notes

  1. Declarations start by stating the function keyword.

  2. Next, the function gets a name. It can be anything and should be camel cased.

  3. Add empty parenthesis or parameter names in the parenthesis.

  4. Put the defining statements inside {}.

  5. Will be hoisted.

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
            └── 2-ControlFlow-and-ErrorHandling
            └── 3-Loops
            └── 4-Functions
                01-declarations.js <----You will be working in this file.

Sample Code

The following function declaration has two parameters:

function addTwoNumbers(a, b){
    console.log(a + b);
    return a + b;
}

Return

Let's look at the structure of the above function, notice how it says return a + b. Let's talk about what return really means! From the docs, The return statement ends function execution and specifies a value to be returned to the function caller. So what does that mean? Well let's break it down:

  • ends function execution: this means that this is the end of the function. Things after the return will not be run, so all code needs to be before the return in order to run.

  • specifies a value to be returned to the function caller: this is what you'll get back from the function, so when you run a function, if you expect it to give you something, that's what return does. So in the above example it is returning the value of a + b.

So, whatever is returned in the function is all that you will get from it. In the above example, if you wanted a - b, you would have to return that instead, because all you're going to get from it is a + b.

Calling the Functions

Here we'll 'call' the function two different times:

addTwoNumbers(5, 7);
addTwoNumbers(5, 6);

Hoisting

Function declarations will be hoisted in JavaScript. This means that the call can come before the function is declared.

//The call
fooFunction(); 

//The declaration
function fooFunction() {
  console.log('foo');
}

Practice

Practice writing 3 different function declarations. You can come up with your own concepts or you can follow our criteria:

  1. Write a function with two parameters(a, b). The function should subtract number b from number a and return the value. You should also log the value to the console.

  2. Write a function with two parameters(first, last). The function should take the two parameters and say hello to a user by their full name. For instance, "Hello, Kenn Pascascio".

  3. Write a function that has one parameter(minutesLeft). The message should print, "You have 5 minutes left in the show."

Previous4-FunctionsNextExpressions

Last updated 7 years ago