0

I am trying to work out why I am getting undefined data type on printName(). So how we can update a global variable trough a function (like a Setter) function and get access the updated value via other functions if needed.

$(function () {
    var name;

    function setName() {
        name = "TestName";
    }

    function printName() {
        alert(name);
    }
    printName();
});
1
  • You don't call the setName function. Commented Mar 16, 2014 at 7:48

2 Answers 2

1

Because you haven't set the name using setName(). function. in other words you never called setName()

Sign up to request clarification or add additional context in comments.

Comments

1
$(function () {
    var name;

function setName() {
    name = "TestName";
}

function printName() {
    alert(name);
}
    setName();
    printName();
});

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.