Search code examples
javascriptobjectnotation

Floats as value in unnamed object


Is it possible to instantiate an unnamed object with floats as values? For example:

var unnamed_object = {key1: 1,555};

This throws an unsurprising compiler exception and I was wondering if it is possible to store floats in this manner. Putting it between quotation marks got rid of the exception but expectedly the value is now type string. Any ideas? Thanks!


Solution

  • Yes, you should use a dot for floating numbers

    var unnamed_object = {key1: 1.555};
    

    By the way, in javascript there is no concept named unnamed object, there is anonymous object:

    someFunction({a:1, b:2}); // it's anonymous object