Search code examples
javascriptenumerate

Are all data types in JavaScript enumerable?


I know that object properties has 3 flags and with one of them I can set a certain property non-enumerable. But in all other cases are all types of data enumerable?


Solution

    • By default all properties created using simple assignment or via a property initializer are enumerable irrespective of data type.

    • Properties defined via methods like Object.defineProperty, Object.defineProperties are by default non enumerable.

    Ex:

    1.Property Initilaizer (by default enumerable):

    const obj = { a: 'foo', b: 123, c: {} };
    

    2.Simple assignment (by default enumerable):

    obj.a = 'foo'
    obj.b = 123
    obj.c = {}
    

    3.Object's methods (by default non-enumerable):

    obj = {};  
    Object.defineProperties(obj, 
        {   a: {   value: 'foo' },
        {   b: {   value: 123 },
        {   c: {   value: {} }
    );  
    

    Further reference here.