SHARE
TWEET

BC2

WhiteofNotGrey Sep 23rd, 2019 108 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Writing reusable code with functions
  2. function ourReusableFunctionName() { //may pass info to (). Function is also being defined as ourReusableFunctionName
  3.     console.log("Hi, World"); //Info inside this curly bracket is run when the function is called
  4. }
  5.  
  6. ourReusableFunctionName(); //function is being called here
  7.  
  8. /*Passing values to functions with arguments
  9.  
  10. Parameters are variables that act as placeholders for values that are to be input to a function
  11. when it's called.*/
  12.  
  13. function ourFunctionwithArgs(a, b) {
  14.     console.log(a - b);
  15. }
  16. ourFunctionwithArgs(10, 5) //when this function is called, data (10, 5) is passed into the function
  17.  
  18.  
  19. /*Global scope and functions
  20. Scope refers to the visibility of variables. These variables are defined/anywhere outside of a
  21. function block have global scope.
  22.  
  23. Global scope refers to them being seen anywhere in JS code.*/
  24. var myGlobal = 10; //this is a global scope, outside a function
  25. function fun1() {
  26.     oopsGlobal = 5; /*this is a GLOBAL Function in a function for NOT having var.
  27.     Var oopsGlobal = 5, This is scoped WITHIN function, aka LOCAL scope*/
  28. }
  29.  
  30. function fun2() {
  31.     var output = "";
  32.     if (typeof myGlobal != undefined ) {
  33.         output += "myGlobal: " + myGlobal;
  34.     }
  35.     if (typeof oopsGlobal != undefined) {
  36.         output += " oopsGlobal: " + oopsGlobal //undefined would show if it was var oopsGlobal
  37.     }
  38.     console.log(output);
  39. }
  40.  
  41. fun1();
  42. fun2();
  43.  
  44. //Local scope and functions
  45.  
  46. function theLocalScope() { /*variables declared within a function/function parameters have LOCAL scope.
  47.                            meaning that they are visible within the function.*/
  48.  
  49.     var myVar = 20   //this variable is declared within the function, hence only visible there.
  50.     console.log(myVar);
  51. }
  52. theLocalScope();
  53.  
  54. /*console.log(myVar); This doesn't work since it tried to access myVar outside the function*/
  55.  
  56.  
  57. //Global vs. Local Scope in Functions
  58. //It's possible to both have GLOBAL and LOCAL variables with the same name.
  59. //The LOCAL variable takes precedence over the global variable
  60. var outerWear = "T-shirt";
  61.  
  62. function myOutfit() {
  63.     var outerWear = "Sweater" //This would appear instead of T-shirt.
  64.  
  65.     return outerWear;
  66. }
  67.  
  68. console.log(myOutfit());
  69. console.log(outerWear) //T-shirt appears here, since it's logging GLOBAL variable.
  70.  
  71.  
  72. //Returing values from a Function with Return
  73. function minusSeven(num) {
  74.     return num -7; //This gives back the result from 10-7
  75. }
  76.  
  77. console.log(minusSeven(10))
  78.  
  79. //Understanding Undefined Value Returned from a function
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top