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

  • Home
  • SEARCH
  • 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 831525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:13:05+00:00 2026-05-15T04:13:05+00:00

if(myVar = img.parent(‘a’).length > 0){ var Y = 1; }else{ var Y = 2;

  • 0
if(myVar = img.parent('a').length > 0){
    var Y = 1;
}else{
    var Y = 2;
}

When I run this code myVar (being announced for the first time) takes the value of img.parent('a').length > 0 and becomes either false or true depending on the case.

First Question:
Is this a correct way of defining myVar?

Second Question:
Am I defining Y for the second time? Is my second 'var' excess?
i.e. should i just write Y = 2;

  • 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-05-15T04:13:06+00:00Added an answer on May 15, 2026 at 4:13 am

    First question: IMO, using an assignment on the condition of the if statement, might cause confusion, also if myVar is not declared previously with the var statement it might become a global variable.

    Second question: No, you aren’t re-declaring Y a second time, actually Y is defined before any assignment, it is hoisted to the top of its enclosing scope.

    This is how actually var behaves in your code:

    var Y; // declared and initialized with `undefined`
    
    if (myVar = img.parent('a').length > 0) {
      Y = 1; // assignment
    } else {
      Y = 2; // assignment
    }
    

    You can observe this behavior with the following example:

    var Y = 'foo';
    (function () {
      alert(Y); //alerts `undefined`
      var Y;
    })();
    

    As you see, the alert is before the var declaration in that function, but since the var statement is hoisted, the Y variable from this new scope is set up before the execution, when the Variable Instantiation process takes place.

    The most straight forward approach, would be to declare and assign myVar:

    var Y, myVar = img.parent('a').length > 0;
    
    if (myVar) {
      Y = 1;
    } else {
      Y = 2;
    }
    // Or Y = myVar ? 1 : 2;
    

    Or even shorter, in a single var statement:

    var myVar = img.parent('a').length > 0,
        Y = myVar ? 1 : 2;
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code $('#first').click(function() { myVar = $(this).next().val(); }); $('#second').blur(function() { console.log(myVar);
Why would you use this syntax? var myVar = myArray.length && myArray || myObject;
I have some Objective-C code that looks like this: #define myVar 10 float f
I have this.... function MyFunction() { var myVar = I think I am encapsulated;
I'm creating html on runtime like this: var myVar = <div id='abc'>some clickable text</div>
I would like to know if something like this pseudo code: myVar = functionName
var myvar = this is the value of myvar; var notMyvar = this is
$(#mySelect).change(function() { var myVar = $('#MyDiv'); for (var i=0;i<this.value;i++) { myVar.clone().appendTo('#formContainer'); } }); Any
Following is the code: var myVar = { test:function(){ alert('hello'); }, myinternalObj: { internalTest:
How can I change image source attribute with var $(this).attr('src','' +myVar+ '') var myVar

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.