declaring an array with "new Array()" vs "[]"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sonic

    declaring an array with "new Array()" vs "[]"

    what exactly is the difference ?

    I think that [] is more suitable for declarative arrays, but not
    exactly sure why.

    TIA

  • Martin Honnen

    #2
    Re: declaring an array with "new Array()" vs "[]"



    sonic wrote:
    [color=blue]
    > what exactly is the difference ?[/color]

    The difference between
    new Array()
    and
    []
    ?
    Both expressions create (not declare!) an empty array. [] is shorter and
    was added to the language later but nowadays browsers that don't support
    the [] syntax are extinct so it is pretty much your choice of preference
    what you use.


    --

    Martin Honnen

    Comment

    • sonic

      #3
      Re: declaring an array with "new Array()" vs "[]"

      thanks,

      so are you saying is that there is absolutely no difference in
      javascript object model between both approaches ?

      Comment

      • Danny

        #4
        Re: declaring an array with "new Array()" vs "[]"



        Correct. Other than semantically, is the same constructor.

        Danny

        On Mon, 11 Jul 2005 10:46:04 -0700, sonic <sonicsoul@gmai l.com> wrote:
        [color=blue]
        > thanks,
        >
        > so are you saying is that there is absolutely no difference in
        > javascript object model between both approaches ?
        >[/color]



        --
        Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

        Comment

        • Douglas Crockford

          #5
          Re: declaring an array with &quot;new Array()&quot; vs &quot;[]&quot;

          sonic wrote:[color=blue]
          > so are you saying is that there is absolutely no difference in
          > javascript object model between both approaches ?[/color]

          There is a difference:

          new Array(30)

          is not the same is

          [30]

          The array literal notation is better.


          Comment

          • sonic

            #6
            Re: declaring an array with &quot;new Array()&quot; vs &quot;[]&quot;

            Do you mind elaborating as to why it is better ?

            Comment

            • VK

              #7
              Re: declaring an array with &quot;new Array()&quot; vs &quot;[]&quot;

              > Do you mind elaborating as to why it is better ?

              Both Array() and [] are equal if you create (oops... "instantiat e") an
              empty array. You can check the set of build-in properties/methods. they
              are equal (because the same constructor is used).

              They treat differently arguments (if you instantiate some array
              elements right away).
              Array(args) scheme is rather tricky, so [args] leaves you lesser place
              for a typo.

              Read more here:
              <http://www.geocities.c om/schools_ring/ArrayAndHash.ht ml#Array_create >

              Comment

              Working...