admin管理员组文章数量:1025244
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);
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);
本文标签: javascriptWhy array push not working in my functionStack Overflow
版权声明:本文标题:javascript - Why array push not working in my function? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745601602a2158488.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论