1

I'm no expert on JS and I try to be humble (once I criticized the triple equality sign and got my fish fried really badly, hehe) so I'd need to verify if the following is a valid JS code at all. It's missing a semicolon and, at first, it looks weirdly but who knows - maybe it's an OK expression anyway and I'm just ignorant.

if (l != "1" || e == "" || g == "") {
  e = null;
  g = null
}

I suspect that it's faulty to the to last assignment to g because I know it's erroneous in regard to the equality signs. But I'd like to have it on record before I mention it to others.

Konrad Viltersten
  • 28,018
  • 52
  • 196
  • 347
  • Yes, valid. JS semicolons can be omitted and the interpreter will insert them, though it is not good practice – Michael Berkowski Feb 17 '15 at 17:19
  • See also: http://stackoverflow.com/q/12745743/50447 – Rowland Shaw Feb 17 '15 at 17:20
  • Semicolons are "optional" but in this case we have inconsistent usage. – Jasen Feb 17 '15 at 17:22
  • possible duplicate of [What are the rules for Javascript's automatic semicolon insertion (ASI)?](http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) – Stephen P Feb 17 '15 at 17:23
  • I assume this is only a fragment? It is syntactically valid, but it won't execute because `l`, `e` and `g` are not declared. – Felix Kling Feb 17 '15 at 17:25
  • FYI, [the answer that you previously chose as the accepted answer](http://stackoverflow.com/a/28567339/456814) was actually plagiarized from [Do you recommend using semicolons after every statement in JavaScript?](http://stackoverflow.com/a/1169596/456814). – Bob Oct 17 '15 at 16:23
  • @Bob I can't see **any** of the replies being accepted as an answer at all. Would you be a sport and help me understand what you were referring to, please? – Konrad Viltersten Oct 17 '15 at 17:37
  • 1
    @KonradViltersten unfortunately I can't, because I can't see deleted answers either, but I've pointed out the original answer that was copied without attribution in my previous comment. It was just to let you know what happened, you're under no obligation to accept a new answer or anything like that if you don't want to. – Bob Oct 17 '15 at 17:50
  • @Bob Aha, that's why I can't see it. It's been deleted. Got it. Well, in such case, we should flag my question as a duplicate of the other (assuming that it's been asked prior to mine, which I'm sure it has because of the copied-over answer). There are badges for approved flaggings so I'm letting you do the honors. :) – Konrad Viltersten Oct 17 '15 at 17:54

2 Answers2

3

JavaScript has a "feature" called automatic semicolon insertion. A newline will usually cause a semi-colon to be implied. So yes, it's valid; whether omitting semicolons is "better" is controversial. I'm used to the good-old days where you'd never omit them, but now there's a trend of "never use semi-colons" in some camps.

Jacob
  • 72,750
  • 22
  • 137
  • 214
0

Actual interpretation of your code will be like :

window.e = null;
window.g = null;
if (l != "1" || e == "" || g == "") {
}

Semicolon won't be a issue in JavaScript.So your code is correct. There are few cases in which not using semicolon is dangerous but this is not your case.

Anshul
  • 8,324
  • 9
  • 53
  • 70
  • Uhm, only variable *declarations* hoist. There is no variable declaration in that code, so hoisting is irrelevant in this example. – Felix Kling Feb 17 '15 at 17:44
  • e = null and g = null, you won't consider them as global variable declaration ? – Anshul Feb 17 '15 at 17:50
  • No, those are assignment expressions. They will implicitly create global variables if `e` and `g` are not declared, but they are not variable declarations (`var ...`) and thus are not hoisted. Hoisting really only applies to function declarations and variable declarations. – Felix Kling Feb 17 '15 at 17:53
  • Agreed that these are assignment operators, but there should be some way to declare global variables, if this is wrong than how will you declare global variables ? – Anshul Feb 17 '15 at 18:00
  • 1
    The better way to declare global variables is to do that explicitly: `window.globalVar = ...;`. However, note that I'm not saying it is "wrong". I'm just saying that there is no hoisting in effect here. And it's pretty easy to test that: No hoisting: http://jsfiddle.net/rbmxjpom/ . Hoisting: http://jsfiddle.net/rbmxjpom/1/. – Felix Kling Feb 17 '15 at 18:02