Learn JavaScript
  • Introduction
  • Basics
    • Comments
    • Variables
    • Types
    • Equality
  • Numbers
    • Creation
    • Basic Operators
    • Advanced Operators
  • Strings
    • Creation
    • Concatenation
    • Length
  • Conditional Logic
    • If
    • Else
    • Comparators
    • Concatenate
  • Arrays
    • Indices
    • Length
  • Loops
    • For
    • While
    • Do...While
  • Functions
    • Declare
    • Higher order
  • Objects
    • Creation
    • Properties
    • Mutable
    • Reference
    • Prototype
    • Delete
    • Enumeration
    • Global footprint
Powered by GitBook
On this page
  1. Functions

Declare

Functions, like variables, must be declared. Let's declare a function double that accepts an argument called x and returns the double of x :

function double(x) {
  return 2 * x;
}

Note: the function above may be referenced before it has been defined.

Functions are also values in JavaScript; they can be stored in variables (just like numbers, strings, etc ...) and given to other functions as arguments :

var double = function (x) {
  return 2 * x;
};

Note: the function above may not be referenced before it is defined, just like any other variable.

PreviousFunctionsNextHigher order

Last updated 4 years ago