if(openmenu != para){
addClass(openmenu , 'collapsed');
}
I am a beginner of JS, I want to add a classname to a element after a if statement. However, if(openmenu != para) and if(!openmenu == para) showed completely different result. Does anyone know why?
!openmenu == para is parsed as (!openmenu) == para, so it first negates openmenu and then checks whether it is equal to para, which is most likely not what you want to do. You could write !(openmenu == para) which would mean the same thing as openmenu != para.
!a == b inverts a's value. For example if it's true, using ! will invert it to false.
if(!true == false) equals to if(false == false) which results as true.
but if(a != b) compares values, lets say a is true and b is false, so if(true == false) => false values are not the same. :)
In this case, if a==null and b==false
so !a = true.
Then !a == b is false but a!=b is true.
I hope that answer is helpful to you.