0

I defined myArr variable in javascript as follows:

var myArr= Array(3);

When I consoled the value of myArr it gave the following output:

[undefined × 3]

When I used the javascript join function which is as follows:

myArr.join('X');

And consoled the output I got the following:

"XX"

Can somebody explain me why I got this output? I was expecting the output to be

"undefinedXundefinedX"
1
  • 1
    undefined is not really a value.. Just a representation of length of array.. Could be read as [NOTHING, NOTHING, NOTHING] Commented Jul 11, 2016 at 5:39

3 Answers 3

1

Array.prototype.join will perform the string conversions of all array elements and joined into one string. If an element is undefined or null, it is converted to the empty string. join

All the undefined elements are equal to ["", "", ""].join('X')

Sign up to request clarification or add additional context in comments.

Comments

1

Array(3) creates an array of three empty holes.

To achieve your desired result, you need to fill the holes: Array(3).fill()

Comments

0

Array(3) will create an array of length 3.

On consoling myArr it will log empty array

join will join all elements of the array into a string

Comments

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.