Im developing a very big application on Scalable Javascript Aplication Architecture, in the core i have a simple function to asign a css property to a element.
establishCSSvalue : function( element, property, value ) {
if( element && property && value ) {
jQuery( element ).css( property, value );
} else {
//log wrong arguments
}
}
If the variable value is equal to 0 the if go to the else, if i delete the value condition o fix the 0:
if( element && property ) {
if( element && property && ( value || value == 0 ) ) {
It works.
Anyone can explain me what its happening, or why? I cant understand or my knowlegde of javascript its not enough, in php is easy with isset. For me a variable with value = 0 exist…
Thanks.
If you are simply trying to see that all three function arguments actually exist and are not undefined, then you can’t just check them for a falsey value as you are. You must specifically check to see if they are not undefined.
You can do that like this:
The reason you cannot do this:
is because any falsey value will cause that statement to fail. falsey values include
null,false,0,"",undefinedandNaN. So, if you ONLY want to check to make sure it’s not undefined, then you need to specifically check for that rather than any falsey value.Note: If all you’re really trying to tell is whether the caller actually passed three arguments to the function when they called it, you can also check
arguments.lengthlike this:or just check to see that the last variable is not undefined: