TIL 9: javascript 3

Standard

Following on from posts on the basics and arrays, another short bit of refresher from freeCodeCamp.

Functions:

function

  • reusable bits of code
  • called or invoked using the function’s name and parentheses, e.g. functionName();
  •  parameters or  arguments can be passed in (as per pic above)

Scope:

  • i.e. visibility of variables
  • Global variables are declared outside of the function block and can be ‘seen’ by the rest of the code
  • omitting the var keyword when declaring a variable automatically makes it global – this can cause unintentional errors!
  • Local variables are those declared within the function, including the function parameters, and are not accessible outwith the function
  • you can create local and global variables with the same name; the local one will take precedence. Again – this can cause many errors for the unwary!
  • use the return keyword to send a value back out of a function
  • the return value of a function can be assigned to a variable, e.g. var solution = functionName(8);
Advertisements

One response »

  1. Pingback: TIL 13: javascript 4 | diary of a scanner

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s