Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9095753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:41:03+00:00 2026-06-16T23:41:03+00:00

Here is my code var a = this.innerHTML; var b = ‘blababibbaib’; if(a !==

  • 0

Here is my code

var a = this.innerHTML;

var b = 'blababibbaib';

if(a !== b)
    {
        c = a;
        return this.innerHTML = b;
    }
else
    {
        return this.innerHTML = c;
    }

and with the var

var a = this.innerHTML;

var b = 'blababibbaib';

if(a !== b)
    {
       var c = a; // here with the var it makes c undefined :-(
        return this.innerHTML = b;
    }
else
    {
        return this.innerHTML = c;
    }

The reason I was doing this is because I wanted a function for an onclick event that would change back and forth between the original and var b. Just for fun really.

But I don’t understand why when you add the var in front of the c variable it makes it undefined once you click through it. Will someone illuminate me?

I’m guessing it has something to do with variable scope when used in functions????

Thanks in advance 🙂

Edit:

Okay, I did this to declare it with var, but I’m still not sure why exactly.

Outside the function I added an if check for c before declaring it

if(!c) var c = '';

But like I said, I would still like to hear whats going on and why Thanks 🙂

Edit 2: Thanks everybody, reading about hoisting now.

I was getting confused I think, it seems even you don’t need to check for c either. Thought might matter…oh well. Thanks again

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:41:07+00:00Added an answer on June 16, 2026 at 11:41 pm

    What is happening in the second example is equivalent to this:

    var a = this.innerHTML;
    
    var b = 'blababibbaib';
    
    var c; // all "var" are hoisted to the top of a function body
    
    if(a !== b)
        {
            c = a;
            return this.innerHTML = b;
        }
    else
        {
            // local c variable not set yet - undefined
            return this.innerHTML = c;
        }
    

    Obviously c is undefined here, since it is output only when it is not set. I suspect what you actually want is:

    var a; //This persists after the function is invoked
    
    myelement.onclick = function () {
        if (!a) { // Only save the value if it isn't already set
            a = this.innerHTML;
        }
        var b = 'blababibbaib';
    
        if (this.innerHTML == b) {
            return this.innerHTML = a;
        } else {
            return this.innerHTML = b;
        }
    };
    

    You can see it here.

    As far as the first snippet is concerned, it works because the value of c is not local to the function, and persists after its invocation. When you assign or refer to a variable in a function body without declaring it using the var keyword, it automatically refers to a property of window with the same name. Consider the following:

    window.c = "Hello, world.";
    
    //Note how there is no local c variable;
    //the c here refers to window.c
    function test1(){
        alert(c);
    }
    
    //There is a c variable local to the 
    //function, so the value alerted is not the value of window.c
    function test2(){
        var c;
        alert(c);
    }
    
    test1(); // alerts "Hello, world."
    test2(); // alerts "undefined"
    

    In the first snippet, you are changing the value of window.c to this.innerHTML whenever the HTML is not "blababibbaib". When the value is "blababibbaib", you are relying on window.c to reset the element’s innerHTML.

    You might want to read up on hoisting in JS, as well as implicit globals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is some code: var class = function(elem,div){ this.elem= elem; this.div = div; this.init
I have this code here: var Person = (function() { var name; var PersonConstructor
I check like this: enter code here var point = new g.LatLng(parseFloat(lat),parseFloat(lng)); var bounds
I've been here and accordingly found out that this piece of code here var
This is my code: var text1 = ↵,<strong>bla bla</strong>;//here text1 has value text1 =
Here in this code: $('#doneItem').click(function(){ $(this).each(function(index){ var item = 'personal' + index; alert(item); localStorage.removeItem('personal'
jsfiddle is here: http://jsfiddle.net/M86nA/ code is this: $('div.wrap').click(function(){ var $minY = $('div.imgur-wrap').height(); if ($minY
Here is my code: var Placemat = function(id,id2,id3) { this.bucket = ; this.result =
I have this JavaScript code: var test123 = $('product-price-' + productId).innerHTML; // thats 26,00&nbsp;€
Here is the JS code: var wrap = document.createElement(div); wrap.innerHTML = '<script type=text/javascript src='+scriptUrl+'></script>';

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.