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,pushfunction show strange behavior?2) mean failure of
cancatfunction?3) this
contactfunction 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
Post a Comment