[Cheat Sheet] Javascript – Functions

Note: This is part of Javascript learning roadmap.

  • primitives are passed by value – if the value is changed in the function, the change will not be visible outside
  • objects are passed by reference – if the object is changed in the function, it will change outside as well
  • arguments can be accessed trough a list arguments inside its scope (arguments[0], arguments.length…)
    •  this way you a function can be called with more arguments than it is formally accepting (and address them arguments[index])
    • the concept of ‘signature of a function’ doesn’t exist in JS – a function can be called with any number of parameters  -> function overloading is not possible as well
    • there is no restriction for how many arguments are passed to a function (the compiler wont’t complain)

 


Declaration

  • function declaration
function sum(n1, n2) {
    return n1 + n2
}
  • function expression
var sum = function(n1, n2) { 
    return n1 + n2
}
  • the main difference between declaration and expression :
    • function declaration are available in an execution context before any code is executed (function declaration hoisting)
    • this is why calling a function which is declared bellow in the code will cause an error (unexpected identifier) for function expression, but not for function declaration
    • therefore, function declaration is recommended

 


Function Scope

  •  variables defined inside a function cannot be accessed from anywhere outside the function – the variable is defined only in the scope of the function
  • a function can access all variables and functions defined inside the scope in which it is defined
  • exception: variables declared without ‘var’ – they belong to the global scope and can be accessed from anywhere (see more)

 


Nested functions

  • The inner function can be accessed only from statements in the outer function
  • The inner function forms a closure: the inner function can use the arguments and variables of the outer function, while the outer function cannot use the arguments and variables of the inner function (way more about closures and how to use them later)

 


Default Parameters

  • after ECMAScript2015, you can define e default value for a param in case it’s not provided in the call
function multiply(a, b=1){
 return a * b;
}
multiply(5); // 5

Note: ECMAScript 2015 (the latest version of JS) is not fully supported an all browsers yet, so using its features make break older browsers.

 


Function Properties

  • each function is an object from data type point of view -> it comes with a set of predefined properties:
    • this – function context (reference the so-called receiver of the call)
    • arguments 
      • list of arguments (it’s not exactly an array because it has other properties as well)
      • arguments.callee – reference to the function that owns the argument object (cool way to reference the function inside the body without explicitly using its name – that makes the refactoring easier)
      • length – number of expected arguments
    • prototype – reference an object – used when the function is a constructor (read more in my other article, class-based vs prototype based languages)


Return Value

  • a function can return any value at any time -> there is no need to specify the return value
  • functions that doesn’t specify a return value -> return undefined
  • it’s recommended that a function either always return a value or never return a value (functions that sometimes return a value create confusions and belong in the pits of hell, not is a JS program)

 

Thank you for your time!

Please subscribe for more weekly web wisdom!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s