5

What is the point/result of calling concat with no parameters? E.g.

Code:

 var board = [
  false, false, false, false,
  false, false, false, false
];
board = board.concat();
6
  • 4
    String board[] isn't JavaScript. Commented Mar 31, 2014 at 9:58
  • 1
    Doesnt look like Javascript indeed. Commented Mar 31, 2014 at 9:58
  • Go through : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 31, 2014 at 9:59
  • concat() is a function not a parameter. Commented Mar 31, 2014 at 9:59
  • sorry, I have edited the post .. Commented Mar 31, 2014 at 10:02

2 Answers 2

6

Concat function is used for concatenation of two Arrays in javascript.

For Example:

a = [1,2,3]
b = [4,5]

a = a.concat(b); // a becomes [1,2,3,4,5]

Edit

Using concat with no arguments can be used to copy an array. For example:

var a = [1,2,3];
var b = a.concat();
b.push(4);

// a is [1,2,3] and b is [1,2,3,4]

Read this from MDN concat source

concat does not alter this or any of the arrays provided as arguments but instead returns a shallow copy that contains copies of the same elements combined from the original arrays. Elements of the original arrays are copied into the new array

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

Comments

1

Note: concat() is a function not a parameter.

If you call concat() without a parameter, you will get a new array as a result. See the summary below.

var board = [ false, false, false, false, false, false, false, false ];
board = board.concat(); // Will return a new copy of your board array.

You may provide an argument to the concat(value1, value2, ..., valueN) function:

N Arrays and/or values to concatenate to the resulting array.


You can read up on the documentation here: Array.prototype.concat() @ MDN

Summary

The concat() method returns a new array comprised of this array joined with other array(s) and/or value(s).

Example

var alpha = ["a", "b", "c"];
var numeric = [1, 2, 3];

// creates array ["a", "b", "c", 1, 2, 3]; alpha and numeric are unchanged
var alphaNumeric = alpha.concat(numeric);

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.