Code:
var testarray = [];
var test1 = "ashutosh";
var test2 = "ashutosh2";
if (test1 != test2) {
testarray.push = "ashutosh3";
testarray.push = "ashutosh4";
alert(testarray.length);
}
if (testarray.length != 1) {
alert(testarray.length);
alert(testarray[testarray.length - 1]);
alert(testarray[testarray.length - 2]);
}
But when all the alerts are showing up undefined. I have no clue why is this happening.
pushis a function, not a property, so instead ofit’s
Here’s how I’d update that code, FWIW, but I think the only substantive change is doing the
pushcorrectly and using>= 2rather than!= 1in thelengthcheck at the end (since otherwise if the array is empty you’re looking at entries-1and-2, which will be undefined):