1

I have a bit of a weird problem. I TRIED to create a jsfiddle but I don't get the same result so I'm sorry I can't share more of what I have.

var parent = id && Number(oldParent) !== 1 ? $('#main_container #item_' + itemId).parent().parent().prev() : null;

This is how I get the parent. It should be null when it isn't needed.

Later, I get this check in the same function:

if (parent && parent != null && !parent.hasClass('.main-group'));
{
    console.log(parent == null);
    var siblingCount = parent.next().children().length;

    if (siblingCount === 0)
    {
        parent.removeClass('group');
        parent.addClass('normal-item');
    }
}

So, I check if parent is set (just in case), parent is not null and parent doesn't have the class main-group. This should work, at least I thought, but I get the error:

TypeError: parent is null

On this row:

var siblingCount = parent.next().children().length;

So, that's why I added the console log to see if parent is null. Guess what? The console.log says true. This means parent is equal to null, but it still goes IN the if-statement. I use && so it shouldn't go in the if statement because already one operation is false.

I had others look at it and they couldn't figure it out either.

Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63

2 Answers2

4

There is a semicolan at the end which is making it to execute as the statement is terminated and next statement executes.

var parent = null;
if (parent && parent != null && !parent.hasClass('.main-group'));{
   alert("Hello");
}

For Debin comment:

var parent = null;
if (parent != null); { alert("Vinoth") }

// The above one is equivalent:

if (parent != null) do nothing ;
alert ("hi");

JavaScript thinks that you have an empty statement and everything to right of it is treated as no longer belonging to the if condition and thus an independent one making it to execute.

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
  • How `;` be there after if ? – Dhara Jan 11 '16 at 12:37
  • Well I'm stupid I guess, don't know why I had a semicolon at the end of the if. I will accept your answer as soon as I can. Didn't see it actually. – Joshua Bakker Jan 11 '16 at 12:39
  • if you do `if (condition);` then it will be an empty statement – Dhara Jan 11 '16 at 12:42
  • well thnx to welcome me btw read http://stackoverflow.com/questions/17036135/why-is-it-that-semicolons-are-not-used-after-if-else-statements..if I am wrong then let meknow..if(); then next statement will be definatly executed..it won't depend on condition which may b OP doesnot want :) – Dhara Jan 11 '16 at 12:43
  • @debin: i am not able to edit my post for some reason, i will update your thought... once its... there – Thalaivar Jan 11 '16 at 12:48
  • @debin: please do look at my answer, which is what you are also thinking – Thalaivar Jan 11 '16 at 12:50
  • @debin: That was my first statement "Welcome to JavaScript"!!!, i guess i missed to add "Weird world" there... – Thalaivar Jan 11 '16 at 12:54
1

You have a ";" at the end of this line

if (parent && parent != null && !parent.hasClass('.main-group'));

This is what is causing the problem.

abisheksampath
  • 376
  • 8
  • 23