Skip to content

Instantly share code, notes, and snippets.

@TRobWE
Forked from anonymous/index.html
Created January 17, 2017 15:48
Show Gist options
  • Save TRobWE/26bd77b181eef8d70ba3af70764111a0 to your computer and use it in GitHub Desktop.
Save TRobWE/26bd77b181eef8d70ba3af70764111a0 to your computer and use it in GitHub Desktop.
FunctionsFunctions: programs within programs!// source https://jsbin.com/qiyimuk
/*
* FUNCTIONS:
*
* Now for a more in-depth study into Javascript functions! We'll start off
* be naming the two phases of using functions. If this two phases talk sounds familiar
* that's because it is variables, our first study also has to phases of use.
* For functions those two phases are declaration, the process of creating the function,
* and invocation, the process of calling or actually using the function. Now, I'll share
* with you the difference between a function's parameters and arguments.
* The parameters of a function are the required inputs decided at the time of
* making the function. These parameters usually have a name that gives a hint to what the
* function is expecting as an argument. Now arguments are the values passed into the
* function at the time of the function being called. Now if you've followed my studies to
* this point you have seen functions in use already, but just in case you missed I included
* the syntax of a Named Function below. Also, in Javascript functions are first class
* objects, which means for one we can store functions in variables. This is also below!
* Functions can optionally take inputs and optionally return a single value. The way we
* do this in Javascript is to for example make an anonymous function called add, which will
* below. With two parameters, these will be our inputs! And the way we could return a
* single value is by adding our parameters in our function! Something to note is that
* in Javascript functions can use and modify variables from global scope, but
* when outside of a blocked scope/function you cannot access variables
* declared inside a blocked scope/ function. An example will be found below from your
* clarification. Now one last thing I will note for functions in Javascript is that
* functions form closures around whatever data is inside it. If you were to return an object
* from a function that object is still in memory somewhere it is being referenced. Which means
* that closure stays alive and the data continues to exist.
*/
function fizzBuzz() {
var arr = [];
for(var i = 1; i <= 100; i++) {
arr.push(i);
if(i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if(i % 3 === 0) {
console.log("Fizz");
} else if(i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
}//A Named Function in all its glory!
var howLong = console.log;
howLong("HELLO"); //prints "HELLO"
//A function stored in a variable! See that's easy!
var add = function(numOne, numTwo) {
return numOne + numTwo;
}
console.log(add(2, 2)); //prints 4
function reference(input) {
input +=10;
}
var copy = 10;
reference(copy); //copy by reference
console.log(copy);
function thisIsScope {
var z = "Hi";
console.log(z);
}
console.log(z); //z will not be defined due to the scope it was declared in
//and the scope we are attempting to use it in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment