From b56d24e1b943b9a209137ba3c2d42b10401c7767 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sun, 21 Aug 2016 15:58:57 -0400 Subject: [PATCH] functions and scope section --- javascript.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/javascript.md b/javascript.md index a5da3c0..04c3471 100644 --- a/javascript.md +++ b/javascript.md @@ -29,6 +29,50 @@ foo = 123.4; ``` ## Functions and Scope + +Declare functions with keyword `function`. Do not need to specify return type. + +```javascript +function myFunc(){ + return 5; +} +``` + +To execute a function, invoke its name with parentheses + +```javascript +myFunc(); +``` + +Can pass parameters into functions + +```javascript +function addFive(numberToAddFiveTo){ + return numberToAddFiveTo + 5; +} +addFive(6); // returns 11 +``` + +Variables declared inside a function cannot be accessed outside of it + +```javascript +function foo(){ + var myVar = 1; + return myVar; +} +console.log(myVar); // error +``` + +but variables declared outside a function are accessible within it + +```javascript +var myVar = 1; +function foo(){ + return myVar; +} +console.log(foo()); +``` + ## Objects/Arrays ## Equality ## Control Flow (loops, if/else)