Home > Net >  i tried adding a figure to my empty array using push
i tried adding a figure to my empty array using push

Time:09-09

I tried using the push to add a number to my empty array and its not working, at all. below is my code

 var giv = []
    var mes = (2, 3);
    function outPut () {
    output.push(2,3); 
        
    }
    alert(output);

CodePudding user response:

You seem to confuse variables giv and output, here is how you can get rid of the error:

var giv = []
var mes = (2, 3);
function outPut () {
    giv.push(2,3); 
}
outPut();
alert(giv);

Output:

[ 2, 3 ]

CodePudding user response:

I think your code is all messed up in terms of variables and function, here is what I think you're trying to achieve. Is that correct?

var giv = [];

function output (input) {
    giv.push(input);
}

var mes = 2;
output(mes);

mes = 3;
output(mes);

alert(giv);

  • Related