1

Coming from Coffeescript im quite used to have anonymous functions assigned to variables, so im for now following the convention:

var sayName = function () {
  var name = "John Doe";
  console.log(name);
};
sayName();

This however triggers an error on JSLint: Unexpected 'sayName', am I missing something?

Luis Martins
  • 1,311
  • 2
  • 15
  • 29
  • It is not really, what you're asking, but have you tried JSHint instead? JSLint has a few very peculiar things it reports. Also, JSHint seems to be more actively maintained. – amenthes May 24 '14 at 18:01
  • I have, although I would prefer for now sticking to JSLint. I might have to rethink that though. – Luis Martins May 24 '14 at 18:02
  • 3
    Try putting a semicolon at the end of the `var functionVar...}` line. – Joe May 24 '14 at 18:07
  • 1
    @LuisMartins - Why have you edited the semicolon into the question? It makes the answer appear incorrect. If the missing semicolon was not the cause of the error you should comment on the answer and explain why it doesn't work for you. – James Allardice May 24 '14 at 22:22
  • @JamesAllardice, I was getting responses suggesting to try that so i though I should correct it since it has no relation to the issue. I apologise if that goes against good practice around SO. – Luis Martins May 25 '14 at 15:06
  • @LuisMartins But to James' point, there's no way you're getting `Unexpected 'functionVar'` in the edited question's code. What error are you getting now? Minus the `console.log`, there's nothing "wrong" with your current snippet. – ruffin May 25 '14 at 22:49

1 Answers1

3

You are missing a semicolon at the end of the block:

var functionVar = function () {
  var name = "John Doe";
  console.log(name);
};
functionVar();

Also, you are defining functionVar at runtime, so JSLint may not get it's being declared before being used.

JSHint shouldn't complain about it, although you may have to review your directives (it complains about not declaring all your vars using var just once at the beginning of a block, for example...)

For more info, check: var functionName = function() {} vs function functionName() {}

Community
  • 1
  • 1
NotGaeL
  • 7,842
  • 3
  • 35
  • 64
  • Can't tell what you mean by, "JSLint may not get its being declared before being used". JSLint is happy with your code. ;^) Do you mean using `var` on the same line that it's defined? As long as you don't have a previous `var` in the same scope (or if the scope is global), [JSLint](http://jslint.com) won't complain. – ruffin May 25 '14 at 22:52
  • I wasn't specific enough. Two separate things: I didn't try it on JSLint so by "not get it's being declared" I meant "if you still have trouble maybe JSLint is not being able to properly detect runtime definitions: Try JSHint". About using `var` I meant what you wrote: just one `var` per scope (or use the proper configuration option(s) to disable the check(s), although onevar in particular http://www.jshint.com/docs/options/#onevar is deprecated). I don't remember JSLint having so many var definition checks, so when switching to JSHint something to worry in general, not this particular chunk. – NotGaeL Jun 01 '14 at 13:12
  • Yep, JSLint does what `onevar` apparently [will soon have] used to do, though you can actually put the `var` line anywhere in the scope you want currently in JSLint, as long as it's before the first variable usage. Thanks for the follow-up! – ruffin Jun 01 '14 at 17:29