1

Following a nodejs course(NodeJS in Action - manning's book) I saw this piece of code

// create server
var server = http.createServer(function(request, response){
    var filePath = false;
    if (request.url == '/') {
        filePath = 'public/index.html';
    } else {
        filePath = 'public' + request.url;
    }
    var absPath = './' + filePath;
    serveStatic(response, cache, absPath);
});
server.listen(3000, function(){
    console.log('Server listening on port 3000.');
});

My question is about the variable "filePath". It is being initialized to false, but later it takes string values.

The editor I use (Visual Studio Code) shows me a warning:

Type 'string' is not assignable to type 'boolean'

My knowledge of javascript is not so deep, but I know it is loosely typed. So here is my question.

Is this initialization to false because of something? Any reaston to do so? Is my editor's warning wrong...? Or just the author's common practice.

4
  • I know this doesn't answer your question but why not use an empty string as the default value for filePath?
    – Aniket
    Aug 8, 2015 at 7:04
  • @Aniket As I said, this is a snippet of code I took from the book. And I also wonder it might be just the author's choice.
    – blfuentes
    Aug 8, 2015 at 7:05
  • 2
    This is definitely an author's choice. JavaScript is loosely typed and this should not throw any errors. If I was the author, I would have used an empty string of type consistency.
    – Aniket
    Aug 8, 2015 at 7:07
  • 1
    it's your editor, the code should work fine, it's just not a good practice
    – user2844991
    Aug 8, 2015 at 7:53

1 Answer 1

3

There is no reason for the initialization to false and it is not even a good practice in this case.

This line:

var filePath = false;

can just be safely changed to this:

var filePath;

This can be done because the if/else in the following statement is guaranteed to initialize filePath one way or the other.

And, I would agree, it's very odd to initialize it to a meaningless boolean value too that doesn't even have an appropriate meaning for the context. It's not even just being over cautious - it's just wrong. It appears that someone thought all declared values should be immediately initialized (which is not the case at all because in Javascript undefined is a perfectly meaningful initial value in some contexts) so they just picked some value to assign to it.

Your editor is providing a warning that it's an unusual situation for you to abruptly switch types in a single variable. It isn't technically an error in Javascript (in other words, it's allowed), but it also could be an indicator of a coding mistake so that's all your editor is telling you.

3
  • Just wondering, because empty string is also false... I couldn't find the point about this initialization to false.
    – blfuentes
    Aug 8, 2015 at 7:55
  • @blacai - an empty string is falsey mean if ("") evaluates to false. That is not quite the same thing as false. But undefined is also falsey which is the value it has with no initialization so there's really no reason to initialize it to false.
    – jfriend00
    Aug 8, 2015 at 8:06
  • yes I know about false == "" and false === "" . Your explanation was very clear.
    – blfuentes
    Aug 8, 2015 at 8:22

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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