Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm fairly new to JavaScript and I wonder if there is a way to write this function shorter and better?

   function checkFile(file){       
        var cbEnab= true;
        if (file !== undefined && file !== null) {
            cbEnab = (!jQuery.endsWith(file, "Com.js"));
        }
        return cbEnab;
    }
share|improve this question
    
I'm only a beginner of sorts in Javascript myself, but I believe you could write the evaluation as if (!file). Also, the bracers around the definition of cbEnab seem unnecessary, don't they? –  Ivo Coumans Feb 10 at 9:45
    
@IvoCoumans if (!file) would work, but changes the behavior a bit. Eliminating braces would also work, but it's poor programming practice. –  200_success Feb 10 at 15:56
    
I actually meant the parentheses (), not the curly braces {}. Didn't know that !file would be different! –  Ivo Coumans Feb 11 at 7:24

1 Answer 1

up vote 4 down vote accepted

By mechanical transformation, I arrive at:

function checkFile(file) {
    return file == null || !jQuery.endsWith(file, "Com.js");
}

The use of === strict equality checking is not necessary; == lumps null and undefined together.

I have my doubts about the whole function, though, as the function name gives no indication that it's going to check whether its argument ends in "Com.js".

share|improve this answer
    
Thanks but do you see that if the file is null I provided true as defulat ,so Im not sure that your code cover this... –  shopia T Feb 10 at 13:04
    
@shopiaT Why are you unsure? –  200_success Feb 10 at 15:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.