Pop Quiz

Every non-trivial language has "gotchas". Here's one. Consider the following JScript, which creates an object and sets some fields to zero:

var x =
  0       : 0,
  123     : 0,
  1.23    : 0,
  blah    : 0,
  "dude!" : 0,
  "-1.23" : 0

A little weird, but it works just fine. How about this?

var x = { -1 : 0 };

That fails with the message "expected identifier, string or number".

Pop quiz: what the heck is going on here? Surely that is an "identifier, string or number", no?

Comments (21)

  1. Michael Ruck says:

    I guess it attempts to create a named field "-1" and set it to 0. However since this is an invalid field identifier, it fails.

  2. Eric Lippert says:

    Bzzt. Try again. As you can see above, "-1.23" is a valid field identifier. Why should -1 be illegal when "-1.23" is legal?

  3. yipyip says:

    Negative indices not allowed?

  4. It’s seems it’s evaluating the minus sign as an operator instead of an indication of "below zero". Thus, it’s looking for an identifier, string or number before the minus to get the left hand side of the subtraction operation.

  5. Because "-1.23" is a string and -1.23 is a float? Since I’m not a jscript kinda person I’m not sure.

  6. Eric Lippert says:

    Larry: But in the example above, 1.23 works, so it can’t be a float vs string issue.

  7. G Man says:

    The 1.23 works because it is positive. The "-1.23" works because it is in quotes. I think the people above had it right. I’m just summing up what they said.

  8. because "-1.23" is a string, and -1 isn’t.

    My best guess, is that in Javascript 1.23 is a number, and – is a high priority operator. Hence -1.23 is not a number, but an expression. I also found that if +1 is the key, rather than just 1, then it also fails with the same error message.

  9. Eric Lippert says:

    Anderson got it.

    Holy cow, twenty-six minutes from question to correct answer. Don’t you people have jobs? 🙂

    Anything in quotes is a string, and any legal identifier is a legal identifier, but a number preceded by a negative sign is an expression, not a numeric literal.

    There is a similar gotcha in VBScript:

    print typename(-32767-1) ‘ Integer

    print typename(-32768) ‘ Long

    A short integer can hold any number from -32768 to 32767, so why is the second example a long? Because -32768 is the negative operator applied to a literal long, not a literal short.

  10. Oh, and Eric: I’m trolling blogs from home right now waiting to take daniel to his dress rehearsal 🙂

    That’s why I have time for this.

    You should look at the time stamps on my "what’s wrong with this code" posts – they’re scary 🙂

  11. Nicholas Allen says:

    Since we’re talking about the quirks of numbers in object initializers…

    Why doesn’t

    var x = { 08 : 0 };

    whine about being a malformed octal literal?

    var y = 08;


  12. Eric Lippert says:

    I don’t get any error for either of those. Or are you talking about JScript .NET?

    I talked a bit about malformed octal here:


  13. Nicholas Allen says:

    JScript .NET (jsc 7.10.3052 to be exact)

  14. Eric Lippert says:

    Ah, how interesting. That’s a bug.

    I added the warning to the code path where an octal literal is evaluated as part of a numeric expression. Since as we’ve just seen, a label must not be an expression, the warning is never hit.

    In this case we’re not looking to evaluate an expression as a number. We need that thing as a canonical string. So we need to turn it into a number, and then turn it back into a string. For some odd reason, the routine that the compiler uses to do that is the same routine as is used at runtime by parseInt, rather than the routine used by the compiler in the main line expression evaluator. parseInt, being a runtime function, doesn’t produce compile-time warnings.

    I’m not sure why the compiler author made that choice — I didn’t write that part of the compiler.

  15. James Mastros says:

    For a while, perl had a bug whereby floating point numbers would be interpreted slightly differently depending on where they were used — it hit very rarely, and only effected the last digit. The problem? Two different functions were being used, one for compile-time constants, one for arithemetic on strings holding floating-point numbers, that did the conversion ever-so-slightly differently. Perl has a much looser compiletime/runtime dichtomy then many languages, and will happily give you warnings at runtime when it feels the need, so that’s not an issue there, but the lesson is still important — stuble differences in behavior aren’t generally a good thing, when your users expect two things to work the same way, and if you implement something twice, you’ll end up with subtle differences in behavior.

  16. Nicholas Allen says:

    Interesting. That is kind of weird to use a runtime function to do the conversion.

    We check for bad octals in the scanner so the warning has already gone out before anyone gets their hands on the token.

  17. Eric Lippert says:

    Right — James has hit the nail on the head. There’s only one place in the JScript code that converts strings to numbers, and that’s the same code that is used in parseInt/parseFloat/etc, so that they remain consistent with each other. (Though there are in fact some oddities, which might be a good subject for a future entry.)

    Your design is better — the warning should have gone into the scanner, not into the portion of the parser which turns numeric literal tokens into expression nodes in the abstract syntax tree.

  18. Steven Bone says:

    Eric – talk about response times… 11:43 AM Nicholas Allen reports a possible bug. 12:01 PM Eric Lippert finds the internal source code, determines the problem, adds a warning in the code, and writes a response.

  19. Eric Lippert says:


    I’m not actually going to FIX it, mind you. I don’t own that code anymore. Next time I see the owner, I’ll mention it to him, but this is a pretty low-priority fix.

Skip to main content