javascript - Unexpected behavior shown by the function concat and push -


i testing code in goolge console , found concat() not working have illustrated below:

var = ["a"]; //undefined a.concat("b","c"); // ["a","b","c"]  

now when push other string string replaces indexes of "b" , "c"

that is[continued]

 a.push("e","f"); // 3   // ["a", "e","f"] 

did notice 3 in line string pushed. interesting me @ first contact "b" , "c" , then, when try value of 1 index return undefined! , then, when push "e" , "f" in same array these string replaces indexes of concated string. question is:

1) why these concat , push function show strange behavior?

2) mean failure of cancat function?

3) thiscontact function nominal?

this correct. concat isn't modifying array expect to.

when you:

a.concat("b","c"); 

it returns array of ["a","b","c"], aren't saving reference (which this)

a = a.concat("b","c"); 

some info from mdn:

concat not alter or of arrays provided arguments instead returns shallow copy contains copies of same elements combined original arrays.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -