I've made a simple function that adds a value to the array in javascript and then returns them.
What I can't return is the added value. What am I doing wrong?
It returns "c" instead of 3.
Fiddle /
Code:
function test(a, b, c) {
var array = [a, b];
array.push('c');
alert(array);
}
test(1, 2, 3);
I've made a simple function that adds a value to the array in javascript and then returns them.
What I can't return is the added value. What am I doing wrong?
It returns "c" instead of 3.
Fiddle http://jsfiddle/0rapj8y8/2/
Code:
function test(a, b, c) {
var array = [a, b];
array.push('c');
alert(array);
}
test(1, 2, 3);
array.push(c);
- no ''
- when you enclose c
in quotes in it is treated as the string literal c
, since you want to push the value referred by the variable c
don't enclose it
– Arun P Johny
Commented
Oct 1, 2015 at 8:04
Very basic language syntax issue. Why do you quote a variable name?
array.push('c');
That is a character c
, not your variable c
array.push(c); // that is now your variable c
Fiddle
Remove the quotes
function test(a, b, c) {
var array = [a, b];
array.push(c);
alert(array);
}
test(1, 2, 3);
Remove Quote in push fuction as follows
function test(a, b, c) {
var array = [a, b];
array.push(c);
alert(array);
}
test(1, 2, 3);