0

I'm getting an odd code syntax warning in Visual Studio, for a bit of JavaScript:

If(1==1) {
  alert("!");
}

The warning is on the opening curly brace:

TS1005 (JS) ';' expected.

I tried adding a semicolon after the closing curly brace, but that didn't make a difference:

enter image description here

I've also determined that when this if block is included anywhere in the script—with or without the trailing semicolon—the entire script fails to execute in the browser (Chrome, latest version).

According to W3Schools, the syntax is correct.

This seems very strange. What's going wrong here?

InteXX
  • 6,135
  • 6
  • 43
  • 80

1 Answers1

1

If should not have a capital letter. If is recognized as a function by VS and therefore expects a ;. The warning should be fixed by using if instead of If. I tested it locally on my VS IDE and using If generated the same warning, but if is fine.

someRandomDev
  • 561
  • 6
  • 15
  • 1
    The warning is correct – `If(1==1)` is parsed as a function (since `If` is not a keyword), which would be followed by a semicolon, newline, or a binary operator. Since the syntax checker doesn’t know that the intent was to write `if(1==1)`, it emits an error when it encounters the `{` character. – MTCoster Oct 05 '20 at 16:07
  • Yikes! I got bit by the case-sensitivity bug (yes, bug). This episode further supports my rant that case sensitivity has no business in a programming language. – InteXX Oct 05 '20 at 16:09
  • @InteXX and there will be 99 other people that tells you it belongs. There is a reason why languages https://stackoverflow.com/questions/503218/why-are-many-languages-case-sensitive – epascarello Oct 05 '20 at 16:12
  • According to that answer, it's a [painful] relic from a bygone era. | @epascarello – InteXX Oct 05 '20 at 16:17
  • When you work at a big organization with tons of developers working on a project, you want uniform code. You do not want to look at one file and see IF() and another iF() and another If() and another if()..... There is a reason we all run linters and have prettiers installed. I am sure if you want to code whatever syntax, than write a transpiler and have it correct the "typos" – epascarello Oct 05 '20 at 16:17
  • *"we all run linters and have prettiers installed"* Since you're running those anyway, you don't need case sensitivity. It's redundant and unnecessary (and painful, as this espisode illustrates). | @epascarello – InteXX Oct 05 '20 at 16:20
  • LInters are verifying the rules for the organization. They are making sure you follow the patterns. Prettier is making the formatting changes. So you can do the same thing that prettier does.... Instead of having it fix spaces, indenting, semicolons, brackets, it fixes your typos with if statements. – epascarello Oct 05 '20 at 16:23
  • I'm still looking for sufficient justification for case sensitivity in a programming language. I haven't found one yet. | @epascarello – InteXX Oct 19 '20 at 16:08
  • Nice snark :-) | @epascarello – InteXX Oct 19 '20 at 20:02