Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I know the maximum array size is 6. So what is the best practice to declare an array?

In the following code I used it like this:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var marks = new Array(6);
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>
</body>
</html>

My understanding is that in this case, a memory location will be allocated at initialization time itself, and so while running, it will just assign the data to the array, which will provide some better performance. But if it will not take the 6 data, some memory space will be wasted.

And another way of implementation is:

<script>
var marks = []
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>

Here memory will be allocated dynamically when assigning the variable, so memory will not be wasted.

I know declaring an array with new() constructor is not a good practice (as mentioned here) as mentioned here)), but if we know the maximum size, I want to know about the best practice. So what is the best practice here for declaring an array in JavaScript?

I know the maximum array size is 6. So what is the best practice to declare an array?

In the following code I used it like this:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var marks = new Array(6);
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>
</body>
</html>

My understanding is that in this case, a memory location will be allocated at initialization time itself, and so while running, it will just assign the data to the array, which will provide some better performance. But if it will not take the 6 data, some memory space will be wasted.

And another way of implementation is:

<script>
var marks = []
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>

Here memory will be allocated dynamically when assigning the variable, so memory will not be wasted.

I know declaring an array with new() constructor is not a good practice (as mentioned here)), but if we know the maximum size, I want to know about the best practice. So what is the best practice here for declaring an array in JavaScript?

I know the maximum array size is 6. So what is the best practice to declare an array?

In the following code I used it like this:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var marks = new Array(6);
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>
</body>
</html>

My understanding is that in this case, a memory location will be allocated at initialization time itself, and so while running, it will just assign the data to the array, which will provide some better performance. But if it will not take the 6 data, some memory space will be wasted.

And another way of implementation is:

<script>
var marks = []
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>

Here memory will be allocated dynamically when assigning the variable, so memory will not be wasted.

I know declaring an array with new() constructor is not a good practice (as mentioned here)), but if we know the maximum size, I want to know about the best practice. So what is the best practice here for declaring an array in JavaScript?

edited title
Link
konijn
  • 34.2k
  • 5
  • 70
  • 267

Best practice to declare an array in JavaScript if we know the Declaring arrays knowing their maximum array size

Post Reopened by Mathieu Guindon, chillworld, Vogel612, Nikita B, unholysampler
added 21 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

For example iI know the maximum array size is 6. So what is the best practice to declare an array?

In the following code I used it like this:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var marks = new Array(6);
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>
</body>
</html>

My understanding is, In that in this case, a memory location will be allocated at initialization time itself, and so while running, it will just assign the data to the array. It, which will provide some better performance. But if it will not take the 6 data, some memory space will be wasted.

And another way of implementation is:

<script>
var marks = []
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>

Here memory will be allocated dynamically when assigning the variable. So, so memory will not be wasted.

I know declaring an array with new()new() constructor is not a good practice (Asas mentioned here) ), but i if we know the maximum size, I want to know which isabout the best practice. So what is the best practice here to declarefor declaring an array in JavaScript?

For example i know the maximum array size is 6. So what is the best practice to declare an array? In the following code I used like this

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var marks = new Array(6);
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>
</body>
</html>

My understanding is, In this case memory location will be allocated at initialization time itself and so while running it will just assign the data to the array. It will provide some better performance. But if it will not take the 6 data some memory space will be wasted.

And another way of implementation is

<script>
var marks = []
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>

Here memory will be allocated dynamically when assigning the variable. So memory will not wasted.

I know declaring array with new() constructor is not a good practice As mentioned here but i if we know the maximum size I want to know which is the best practice. So what is the best practice here to declare an array in JavaScript

I know the maximum array size is 6. So what is the best practice to declare an array?

In the following code I used it like this:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var marks = new Array(6);
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>
</body>
</html>

My understanding is that in this case, a memory location will be allocated at initialization time itself, and so while running, it will just assign the data to the array, which will provide some better performance. But if it will not take the 6 data, some memory space will be wasted.

And another way of implementation is:

<script>
var marks = []
marks = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = marks[3]; 
</script>

Here memory will be allocated dynamically when assigning the variable, so memory will not be wasted.

I know declaring an array with new() constructor is not a good practice (as mentioned here) ), but if we know the maximum size, I want to know about the best practice. So what is the best practice here for declaring an array in JavaScript?

deleted 6 characters in body
Source Link
Rameez
  • 165
  • 7
Loading
Added the code and give more clarifiction in question
Source Link
Rameez
  • 165
  • 7
Loading
Post Closed as "Not suitable for this site" by Simon Forsberg, Donald.McLean, Mathieu Guindon, RubberDuck, Marc-Andre
Source Link
Rameez
  • 165
  • 7
Loading
default

AltStyle によって変換されたページ (->オリジナル) /