1

My question may be very easy to lots of people, but I am new to Javascript. I really do not know what is wrong with the following codes.

var newValue = 1;
function getCurrentAmount() {

return [newValue,2,3];
}
var result = getCurrentAmount();
console.log(result[0] + "" + result[1] + result[2]);

In the above code, the result shown in console is: undefined23 Why is the result not "123"? I am trying to use global variable because I want to increment newValue by 1 each time when the function is called. I want something like the following:

var newValue = 1;
function getCurrentAmount() {
newValue ++;
return [newValue,2,3];
}
setInterval(function(){
   var result = getCurrentAmount();
    console.log(result[0] + "" + result[1] + result[2]);
}, 1000);

Also, I just tired the following codes and it works as expected.

    var newValue =1;
    function test() {
    newValue ++;
    return newValue;
}

console.log(test());

So I think the problem is about the Array.

I hope my question is clear enough. Thanks in advance.

4
  • I get 123. Do you have other code that might be interfering? This would be the correct way to use 'globals' by the way. Commented Nov 28, 2012 at 16:59
  • I too get 123 on chrome..jsfiddle.net/D9VP4 Commented Nov 28, 2012 at 16:59
  • 1
    Is the order of the code as in your example? This can happen sometimes when the code is in the wrong order, or you don't take into account the onLoad or DOMReady events. Commented Nov 28, 2012 at 17:05
  • I just found out the problem. Jav Rok also found my problem as well. I don't get what I expected because of wrong order. I am a Java coder. JAVA does not care about the order but Javascript does. Putting the var newValue = 1 in the very beginning fixed my problem. Thanks for everyone's help. Commented Nov 28, 2012 at 17:08

5 Answers 5

2

A better approach whould be to shield newValue from the global scope by using closures. Like so:

var getCurrentAmount = (function () {
    var newValue = 1; // newValue is defined here, hidden from the global scope
    return function() { // note: return an (anonymous) function
        newValue ++;
        return [newValue,2,3];
    };
)()); // execute the outer function
console.log(getCurrentAmount());
Sign up to request clarification or add additional context in comments.

1 Comment

My problem is caused by disorder, someone has commented it above. However, no answer here pointed it out. I will accept your answer as the solution because you taught me another good way that I did not know. Thanks for your answer.
0

You can implement a "sort of static" variable like so:

function getCurrentAmount() {
    var f = arguments.callee, newValue = f.staticVar || 0;
    newValue++;
    f.staticVar = newValue;
    return [newValue,2,3];
}

This should work better than your global variable approach.

1 Comment

This works but the dependency on callee.staticVar is very implicit. Better to use a closure.
0

The code you have given behaves as you would expect, not as you report. Here's a jsfiddle that demonstrates.

You must have set newValue in a different context from what you're showing in your question.

Comments

0

this code works for me:

var newValue = 1;
function getCurrentAmount() {

return [newValue,2,3];
}
var result = getCurrentAmount();
console.log(result[0] + "" + result[1] + result[2]);

take a look here : http://jsfiddle.net/PAfRA/

Comments

0

The code you say it doesn't work it's actually working, see working demo , so if it's not working for you it's possible you don't have newValue variable at the global scope (ie. at the root of your js file and not inside any other function).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.