Skip to content

Instantly share code, notes, and snippets.

@Chryus
Last active January 4, 2016 01:18
Show Gist options
  • Save Chryus/8547016 to your computer and use it in GitHub Desktop.
Save Chryus/8547016 to your computer and use it in GitHub Desktop.
> var first_name = function (name) {
... return name;
... }
undefined
> first_name("bob");
'bob'
//first_name is a variable with an anonymous function as its value. we assign
> function add(x,y) {
... return x + y;
... }
undefined
> add(2,3);
5
//add is named function with two paramaters; it returns the sum of the params.
> full_name = function(first_name, last_name) {
... full_name = function (first_name, last_name) {
> full_name = function(first_name, last_name) {
..... return first_name + ' ' + last_name
..... }
[Function]
> full_name
[Function]
> typeof(full_name);
'function'
> full_name("jasper", "bunz")
'jasper bunz'
//full_name is a variable with an anonymous function as its value that concatenates two strings. it explicitly returns the strings.
> function cube (x) {
..... return(x * square(x));
..... }
undefined
> console.log(cube(2));
8
//the square function is called inside the cube function and inherits its params.
> function my_fun() { return "I am having fun" };
undefined
> my_fun("chris")
'I am having fun'
//like a wizard, you can invoke the function by typing its name and parentheses. Notice here that JavaScript
//didn't throw an error even though I passed it a parameter that wasn't prescribed in the function. JavaScript
//wants to make the code work.
> var what = function () { return "HI!!!!" };
undefined
> console.log(typeof(what));
function
//say what? I thought this was a variable with a function as its value but JavaScript is saying this is a function?
//i guess JavaScript equates that as a function.
> var max_value = function(array) {
... var result = array[0];
... for (var i = 0; i < array.length; i++) {
..... if (array[i] > result) {
....... result = array[i];
....... };
..... }
... return result;
... }
undefined
> console.log(max_value([1, 222, 132, 33]));
222
//this named function iterates over an array an returns the greatest value in the array. inside the function, a
//local variable result is declared and set to the first element in the array. we then loop through each element in the array and compare it to the first element via the > operator. If the element is greater than the first element, we reassign that value to the local variable and explicitly return it at the end.
> var arr = ["boy", 42, 23, function () { return "gotta catch 'em all, Pokemon!" }, 56];
undefined
>
undefined
> console.log(arr[3]());
gotta catch 'em all, Pokemon!
//arr[3] access the third element in array arr an arr[3]() invokes that element (because it is a function).
> function kesha() {
... return "Your love is my drug";
... }
undefined
> kesha();
'Your love is my drug'
// we invoke function by typing their names with parentheses after.
> var lambchop = function () {
... return "This is the song that never ends";
... }
undefined
> console.log(lambchop());
This is the song that never ends
//since we explicity return a string inside the lambchop function, the string is returned.
> function doctor_name (last_name) {
... return "Dr. " + last_name
... }
undefined
> console.log(doctor_name());
Dr. undefined
//Lol ok JavaScript really really really wants to evaluate code so when the function is invoked after console.log, it sees //that no param was given, so it assigns it an undefined value. Then when we concatenate the string "Dr." with the value //undefined, undefined is converted to a string. So the literal string "Dr. undefined" is returned.
> function dwelling(name) {
... if (typeof(name) != "string") { throw "ArgumentError" };
... return name + " cave";
... }
undefined
> console.log(dwelling(42));
ArgumentError
//In our control flow we check for type. since the number 42 isn't a string, JS throws the error.
> function add(x, y) {
... return(x + y);
... }
undefined
> console.log(add(1, 2, 3, 4, 10, 20));
3
// Again, JS wants to evaluate our code, so it ignores the extra params.
> console.log(args(8, 7, 6, 5, 4));
{ '0': 8, '1': 7, '2': 6, '3': 5, '4': 4 }
undefined
> typeof(console.log(args(8, 7, 6, 5, 4)));
{ '0': 8, '1': 7, '2': 6, '3': 5, '4': 4 }
'undefined'
//this one is strange. arguments is a special keyword in JS and it returns an undefined, array-like structure that is an object. it represents all the arguments that are passed to a function.
> function lamp() {
... var my_special_variable = "I am special";
... }
undefined
> console.log(my_special_variable);
ReferenceError: my_special_variable is not defined
//my_special_va
function min_value(array) {
var result = array[0];
for (var i =0; i < array.length; i++) {
if (array[i] < result) {
result = array[i];
}
}
return result;
}
min_value([2, 8, 20, 1, 3]);
//min_value iterates over each element in the array and sets a local variable result equal to the lowest value in the array.
function song () {
return "Mary had a little lamb";
}
song();
//invoke with song();
function add_numbers(num1, num2) {
if (typeof (num1) != "number" || typeof (num2) != "number") {
return "ERROR";
} else {
return num1 + num2;
}
}
add_numbers(12, 14);
//add_numbers function takes two parameters and throws and error if they are not numbers.
arr = [0, "nice", function bad_hairday () { return "YOLO" }];
arr[2]();
//invoke the function in the array
var your_name = function() {
return "Snoop Dog";
};
your_name();
> var result;
undefined
> if (undefined) {
... result = function () { return "blah blah blah"; }();
... } else {
... result = function () { return "meow meow meow"; }();
... }
'meow meow meow'
//IMPORTANT last case
//we have a side effect here. Because result was defined initially to the program it is available everywhere. Inside the function, we reset the value of result to a function; therefore, it is no longer undefined. To avoid side effects, you might think we could just add var to undefined in the block, like this...
> if (undefined) {
... var result = function () { return "blah blah blah"; }();
... } else {
... var result = function () { return "meow meow meow"; }();
... }
undefined
>
undefined
> console.log(result);
meow meow meow
//but that still yields "meow meow meow" because the context is a block. In a block, resetting the value of a variable will reset its value plus the value of any variables with the same name one context above it. the only way to return undefined and not reset the initial var results is to wrap the block in a function and place var in front of result...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment