Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:53:40+00:00 2026-05-16T00:53:40+00:00

Is it better to write var arr=[]; then var arr=new Array(); var obj={}; then

  • 0

Is it better to write

var arr=[]; then var arr=new Array(); 
var obj={}; then var obj=new Object();

and if so, why?

I read slide lection page 36 about that idea, but no explanation was given or example why its better.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:53:41+00:00Added an answer on May 16, 2026 at 12:53 am

    There is not a big difference between these definitions, except that the first way uses the array/object literal and the second the array/object constructor.

    The array constructor may return different results, depending on the number of arguments passed in. If you pass in one argument, a new empty array is created of the length of that argument. For example:

    // arr1 is the same as arr2
    var arr1 = new Array(1, 2, 3, 4);
    var arr2 = [1, 2, 3, 4];
    
    alert(arr1.length == arr2.length); // true
    alert(arr1[0]); // 1
    alert(arr2[0]); // 1
    

    But, passing in one argument results differently:

    // arr3 has length 200 and is empty, while arr4 has length 1 and contains a number
    var arr3 = new Array(200);
    var arr4 = [200];
    
    alert(arr3.length == arr4.length); // false
    alert(arr3[0]); // 'undefined'
    alert(arr4[0]); // 200
    

    The speediest way to define an array or object is of course the literal way, because you don’t need to call the constructor first. Anyway, the actual speed difference is negligible, really.

    I did a speed test in Chrome 6, in which I defined 20 times 10000000 the same array 1, 2, 3, which gave these results:

    Average speed per 10000000 calls
    Array Constructor  : 226.55 ms
    Array Literal      : 159.1  ms
    

    As you can see, the array literal is 67,45ms faster per 10000000 array definitions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 485k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use - (NSRange)rangeOfString:(NSString *)aString. The code will look… May 16, 2026 at 7:46 am
  • Editorial Team
    Editorial Team added an answer This might not be the best answer for you, but… May 16, 2026 at 7:46 am
  • Editorial Team
    Editorial Team added an answer ResourceSpace.Has.ResourcesOfType<object>() .WithoutUri .TranscodedBy<MyCustomCodec>() .ForMediaType("application/json"); That should do it. May 16, 2026 at 7:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.