Toggle variable through a function










0














this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.



Here is it:



var hello = false 

function toggleSt(I, E)

if ((I == "activate") && (!E))

E = !E
alert("activated")

else if ((I == "disable") && (E))

E = !E
alert("disabled")



toggleSt("activate", hello)

alert(hello)


I pasted the code on JSFiddle,



http://jsfiddle.net/kpDSr/



Hello is still false.










share|improve this question



















  • 1




    E will not be a reference to hello, it will only have the same value. Changing E will not change hello.
    – Felix Kling
    Aug 10 '11 at 15:34
















0














this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.



Here is it:



var hello = false 

function toggleSt(I, E)

if ((I == "activate") && (!E))

E = !E
alert("activated")

else if ((I == "disable") && (E))

E = !E
alert("disabled")



toggleSt("activate", hello)

alert(hello)


I pasted the code on JSFiddle,



http://jsfiddle.net/kpDSr/



Hello is still false.










share|improve this question



















  • 1




    E will not be a reference to hello, it will only have the same value. Changing E will not change hello.
    – Felix Kling
    Aug 10 '11 at 15:34














0












0








0







this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.



Here is it:



var hello = false 

function toggleSt(I, E)

if ((I == "activate") && (!E))

E = !E
alert("activated")

else if ((I == "disable") && (E))

E = !E
alert("disabled")



toggleSt("activate", hello)

alert(hello)


I pasted the code on JSFiddle,



http://jsfiddle.net/kpDSr/



Hello is still false.










share|improve this question















this question is pretty straightforward. I want to be able to detect whether a variable is false, and set it to true, commonly known as toggle.



Here is it:



var hello = false 

function toggleSt(I, E)

if ((I == "activate") && (!E))

E = !E
alert("activated")

else if ((I == "disable") && (E))

E = !E
alert("disabled")



toggleSt("activate", hello)

alert(hello)


I pasted the code on JSFiddle,



http://jsfiddle.net/kpDSr/



Hello is still false.







javascript variables toggle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:19









Cœur

17.5k9104145




17.5k9104145










asked Aug 10 '11 at 15:31









ImplosionsImplosions

1114




1114







  • 1




    E will not be a reference to hello, it will only have the same value. Changing E will not change hello.
    – Felix Kling
    Aug 10 '11 at 15:34













  • 1




    E will not be a reference to hello, it will only have the same value. Changing E will not change hello.
    – Felix Kling
    Aug 10 '11 at 15:34








1




1




E will not be a reference to hello, it will only have the same value. Changing E will not change hello.
– Felix Kling
Aug 10 '11 at 15:34





E will not be a reference to hello, it will only have the same value. Changing E will not change hello.
– Felix Kling
Aug 10 '11 at 15:34













2 Answers
2






active

oldest

votes


















1














Felix is right. Try:



var hello = false 

function toggleSt(I)

if ((I == "activate") && (!hello))

hello = !hello;
alert("activated")

else if ((I == "disable") && (hello))

hello = !hello
alert("disabled")



toggleSt("activate");

alert(hello)





share|improve this answer






















  • But that's hardcoding! Any possible alternative?
    – Implosions
    Aug 10 '11 at 15:47


















0














You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.



var hello = false 

function toggleSt(I)

if ((I == "activate") && (!hello))

hello = !hello
alert("activated")

else if ((I == "disable") && (hello))

hello = !hello
alert("disabled")



toggleSt("activate")

alert(hello)





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7013488%2ftoggle-variable-through-a-function%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Felix is right. Try:



    var hello = false 

    function toggleSt(I)

    if ((I == "activate") && (!hello))

    hello = !hello;
    alert("activated")

    else if ((I == "disable") && (hello))

    hello = !hello
    alert("disabled")



    toggleSt("activate");

    alert(hello)





    share|improve this answer






















    • But that's hardcoding! Any possible alternative?
      – Implosions
      Aug 10 '11 at 15:47















    1














    Felix is right. Try:



    var hello = false 

    function toggleSt(I)

    if ((I == "activate") && (!hello))

    hello = !hello;
    alert("activated")

    else if ((I == "disable") && (hello))

    hello = !hello
    alert("disabled")



    toggleSt("activate");

    alert(hello)





    share|improve this answer






















    • But that's hardcoding! Any possible alternative?
      – Implosions
      Aug 10 '11 at 15:47













    1












    1








    1






    Felix is right. Try:



    var hello = false 

    function toggleSt(I)

    if ((I == "activate") && (!hello))

    hello = !hello;
    alert("activated")

    else if ((I == "disable") && (hello))

    hello = !hello
    alert("disabled")



    toggleSt("activate");

    alert(hello)





    share|improve this answer














    Felix is right. Try:



    var hello = false 

    function toggleSt(I)

    if ((I == "activate") && (!hello))

    hello = !hello;
    alert("activated")

    else if ((I == "disable") && (hello))

    hello = !hello
    alert("disabled")



    toggleSt("activate");

    alert(hello)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 10 '11 at 15:45

























    answered Aug 10 '11 at 15:38









    Gabriel RossGabriel Ross

    4,03112127




    4,03112127











    • But that's hardcoding! Any possible alternative?
      – Implosions
      Aug 10 '11 at 15:47
















    • But that's hardcoding! Any possible alternative?
      – Implosions
      Aug 10 '11 at 15:47















    But that's hardcoding! Any possible alternative?
    – Implosions
    Aug 10 '11 at 15:47




    But that's hardcoding! Any possible alternative?
    – Implosions
    Aug 10 '11 at 15:47













    0














    You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.



    var hello = false 

    function toggleSt(I)

    if ((I == "activate") && (!hello))

    hello = !hello
    alert("activated")

    else if ((I == "disable") && (hello))

    hello = !hello
    alert("disabled")



    toggleSt("activate")

    alert(hello)





    share|improve this answer

























      0














      You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.



      var hello = false 

      function toggleSt(I)

      if ((I == "activate") && (!hello))

      hello = !hello
      alert("activated")

      else if ((I == "disable") && (hello))

      hello = !hello
      alert("disabled")



      toggleSt("activate")

      alert(hello)





      share|improve this answer























        0












        0








        0






        You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.



        var hello = false 

        function toggleSt(I)

        if ((I == "activate") && (!hello))

        hello = !hello
        alert("activated")

        else if ((I == "disable") && (hello))

        hello = !hello
        alert("disabled")



        toggleSt("activate")

        alert(hello)





        share|improve this answer












        You assign hello to the new var E when you call the function. So in the function you have the new parameter E that set to true/false. Call the function without a parameter for hello and use hello as a global variable will work as you expected.



        var hello = false 

        function toggleSt(I)

        if ((I == "activate") && (!hello))

        hello = !hello
        alert("activated")

        else if ((I == "disable") && (hello))

        hello = !hello
        alert("disabled")



        toggleSt("activate")

        alert(hello)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 10 '11 at 15:46









        Andreas KöberleAndreas Köberle

        47.6k39177235




        47.6k39177235



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f7013488%2ftoggle-variable-through-a-function%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric