What is the way of declaring an array in JavaScript

0 votes

These are some of the way to create arrays in js:

  • var myArray = new Array()
  • var myArray = new Array(3)
  • var myArray = ["apples", "bananas", "oranges"]
  • var myArray = [3]
What is the difference among them and what is the preferred way?
I read somewhere that the following declarations are completely different
var badArray = new Array(10); // creates an empty Array that's sized for 10 elements
var goodArray= [10];          // creates an Array with 10 as the first element

Is what the authors are attempting to express that [10] generates an array of undefined size with 10 as the 0th element and [10] makes an array of exactly 10 elements? What else does this mean?

Aug 3 in TypeSript by krishna
• 2,540 points
9 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In TypeSript

0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
13 views
0 votes
0 answers

What is TypeScript and why would I use it in place of JavaScript?

Can you please describe what the TypeScript ...READ MORE

Jul 18 in TypeSript by Logan
• 2,140 points
10 views
0 votes
1 answer

What is "not assignable to parameter of type never" error in TypeScript?

All you have to do is define ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
784 views
0 votes
0 answers

What is a type in Typescript for the Component class in Angular 2+?

I have a small problem, but big ...READ MORE

Jul 5 in TypeSript by Logan
• 2,140 points
13 views
0 votes
1 answer

What is the question mark for in a Typescript parameter name?

It is to mark the parameter as optional. TypeScript ...READ MORE

answered Aug 3 in TypeSript by Abhinaya
• 760 points
17 views
0 votes
1 answer

Now I know what 'pick' does. But I want to do the opposite and remove values from an object.

You can use the keyword Omit for ...READ MORE

answered May 27 in TypeSript by Nina
• 3,020 points
20 views
0 votes
1 answer

How to store an array in localstorage?

Localstorage only supports Strings. So you can ...READ MORE

answered Jul 1, 2019 in Others by sunshine
• 1,280 points
18,982 views
0 votes
1 answer

How to store input value into array then localstorage?

Hello @ abhittac, You have create the array everytime the ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,740 points
4,924 views
0 votes
0 answers

For-each over an array in JavaScript

How can I loop through all the ...READ MORE

Feb 8 in Java by Rahul
• 9,000 points
24 views
0 votes
0 answers

Are all Blockchain array implementations incorrect?

I found many Blockchain implementations on the ...READ MORE

Apr 4 in Blockchain by Rahul
• 9,000 points
25 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP