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.

3
  • 6
    Remove the semi-colon at the end of if statement Commented Jan 11, 2016 at 12:36
  • Apart from the semicolon at the end, there is no need for the !=null check as your initially checking if parent is truthy if(parent) a truthy value can't be null. Commented Jan 11, 2016 at 12:39
  • Yes I know but it didn't work so I tried a sh*tload of stuff, I was even checking on null as string since I didn't know anymore. Commented Jan 11, 2016 at 12:40

2 Answers 2

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.

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

7 Comments

How ; be there after if ?
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.
if you do if (condition); then it will be an empty statement
well thnx to welcome me btw read stackoverflow.com/questions/17036135/… 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 :)
@debin: i am not able to edit my post for some reason, i will update your thought... once its... there
|
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.

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.