Last active
August 29, 2015 14:12
-
-
Save bradydowling/07b2cc2c1f35778c8a66 to your computer and use it in GitHub Desktop.
Using Function Declarations with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using a function declaration, I cannot assign different values to a variable conditionally. | |
// All declarations will be hoisted to the top of the code and the latest declaration will apply. | |
var day = 'Friday'; | |
if (day === 'Friday') { | |
function myGreeting () { | |
return "Happy Friday!"; | |
} | |
} | |
else { | |
function myGreeting () { | |
return "Good morning!"; | |
} | |
} | |
console.log(myGreeting()); // Logs "Good Morning!". |
Isn't that what I mentioned in the comment at the top? Or are you referring to something else? I added in the parens necessary to call the function in the console.log so I think I'm all set now. Am I missing something?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Want to have Happy Friday? Run this code in Firefox :)
MDN says that conditional declaration should work according to es3, but now deprecated in es5 strict.