1

How can I create a blank array of length 4 in jQuery. I want to create the array first and later want to punch values in it.

1 Answer 1

3

Use Array Constructor, new Array(arrayLength)

var array = new Array(4);

Note: This array is not essentially blank, it contains undefined but it's length is 4.

If feasible,

var arr = [1, 2, 3, 4];
Sign up to request clarification or add additional context in comments.

2 Comments

and also with var array = ["1","2"] with default values. console.log(array.length)
@Akki619 I've updated answer, but it is not always possible, when you have large number of elements and you want to create array dynamically, instead of loop, Array constructor is better choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.