-3

Could this be a javascript compiler error with else if.

Here is the code;

"use strict"
var me ;
var a=1;
if (a==1)
{me ="hello"}
else if (a==2)
{me ="bye"}
else id (a==3) 
{me ="ups"}
console.log(me)

Note that I have 'id' and not 'if' .

You have a final 'ups' value for 'me' .... And no error....

Checked with chrome & firefox

What is happen ?

civiltomain
  • 1,136
  • 1
  • 9
  • 27
  • 2
    `id (…)` is a function call. It's the only content of the `else` block. Following that is another block with superfluous `{}` which will always be executed. – deceze Sep 05 '16 at 10:06
  • 1
    And because the `else` block is never reached, you're not getting a *"id is not defined"* error either. – adeneo Sep 05 '16 at 10:08
  • @adeneo . De 'd' letter is very near 'f' letter. IMHO the compiler would have to rise an error.....I have not id function.... – civiltomain Sep 05 '16 at 10:23
  • 2
    There's no error if the undefined function call never happens. – adeneo Sep 05 '16 at 10:41
  • It is not just a typo ? id => if ? – aprovent Sep 05 '16 at 10:55
  • 1
    @aprovent Yeeeees…?! And the question is why this code behaves the way it does *with this typo*. – deceze Sep 05 '16 at 11:04

1 Answers1

4

Your code is equivalent to:

…
else {
  id(a == 3);
}

{
  me = "ups";
}

Yes, you can have {} brackets to enclose code blocks even without any if statement or such, it simply won't do anything special in this case. The me = "ups" statement will always be executed because it's not associated with the else clause, the id() function call is the only statement associated with that block.

No, it's not a compiler bug, it's what you wrote.

deceze
  • 510,633
  • 85
  • 743
  • 889