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);
Share
Improve this question
asked Oct 1, 2015 at 8:02
Ionut NeculaIonut Necula
11.5k4 gold badges49 silver badges72 bronze badges
3
-
2
array.push(c);
- no''
- when you enclosec
in quotes in it is treated as the string literalc
, since you want to push the value referred by the variablec
don't enclose it – Arun P Johny Commented Oct 1, 2015 at 8:04 - Hmm..my mistake. Thanks. – Ionut Necula Commented Oct 1, 2015 at 8:05
- I've made it a string. Saw that now. Thank you. – Ionut Necula Commented Oct 1, 2015 at 8:06
3 Answers
Reset to default 4Very 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);