Skip to content

Instantly share code, notes, and snippets.

@bradydowling
Last active August 29, 2015 14:12
Show Gist options
  • Save bradydowling/07b2cc2c1f35778c8a66 to your computer and use it in GitHub Desktop.
Save bradydowling/07b2cc2c1f35778c8a66 to your computer and use it in GitHub Desktop.
Using Function Declarations with JavaScript
// 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!".
@aliaksandr-kazarez
Copy link

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.

@bradydowling
Copy link
Author

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