Javascript missing semicolon/'a' is undefined; please fix or add

workspace
javascript

#1

Hey all,

So I saw there was a thread similar to this but it was dealing more with importing other classes into a file. My question is a simple syntax one:

// in a file named “test.js"
test()
if (typeof a != ‘undefined’) document.write(‘a = "’ + a + '”
’)
if (typeof b != ‘undefined’) document.write(‘b = "’ + b + ‘"
’)
if (typeof c != ‘undefined’) document.write(‘c = "’ + c + ‘"
’)

function test()
{
a = 123
var b = 456
if (a == 123) var c = 789
}

So this is all completely valid JS code and I’ve tried it and it works. However my gutter is littered with warnings about how these lines are all missing semicolons. Not only that, but it tells me that a is not defined and c is defined but never used. I don’t want to turn off warnings like this completely in case I do actually make a mistake later on but they are really annoying right now. Any suggestions?


#2

Hi.

While your code works now, the purpose of the eslint implementation at Cloud9 is to help you code using current coding standards. In your case, you never declare “var a”, meaning that your code, while it works, will fall into this scenario: http://stackoverflow.com/questions/6888570/declaring-variables-without-var-keyword (you can read more on the horrors of global variables in the link provided in the answer).

For disabling eslint on a file-to-file basis, you can use the proposed solution here: http://evanhahn.com/disable-eslint-for-a-file/

I hope this helps!

Regards,
Dana


#3

If you really want to, you can also disable checking for undeclared variables and missing semicolons from project settings


And can fine tune eslint options with .eslintrc


VarName is not defined, please fix or add /*global VarName*/ Cloud9
#4

@harutyun thanks for the help! @dana you are absolutely right and thank you for pointing me in the right direction. It turns out the book I was using to learn JS in the first place is actually pretty outdated! I want to learn best practices so I’m going to try and hold my code to more current standards. Thanks again to both!


#5

try using this book, Eloquent Javascript
It is free and really awesome, and somewhat funny to read.