Skip to content

Instantly share code, notes, and snippets.

@nfroidure
Last active December 14, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfroidure/5005563 to your computer and use it in GitHub Desktop.
Save nfroidure/5005563 to your computer and use it in GitHub Desktop.
Fun Javascript behavior
if(function test(a){console.log(a)}) { console.log(test('test')) }
// ReferenceError : test is not defined
var test;
if(test = (function(a){console.log(a)})) { console.log(test('test')) }
// test
@nfroidure
Copy link
Author

Tested in Google Chrome

@naholyr
Copy link

naholyr commented Feb 21, 2013

Dynamic unnamed function:

var foo = function (a) { console.log(a); };

foo('test'); // OK

Dynamic named function:

var foo = function test (a) { console.log(a); };

foo('test'); // OK
test('test'); // ReferenceError
console.log(foo.name); // 'test'

The, well, standard (?) function:

function test (a) { console.log(a) }

test('a'); // OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment