javascriptoperatorsshort-circuiting

What does the || (logical OR, double vertical line) expression do as a stand-alone statement?


The code below makes sense individually, but i do not get how it all works when it is combined into one statement like shown below.

The line i have trouble understanding is "discard.test(word) || (......" What is the or || operators doing in this statement?

words.forEach(function(word) {
  discard.test(word) ||
    (word = word.replace(punctuation, ""),
      stopWords.test(word.toLowerCase()) ||
    (word = word.substr(0, maxLength),
      tag_index[word.toLowerCase()] = word,
      tags[word = word.toLowerCase()] = (tags[word] || 0) + 1));
});

Solution

  • Basically it is this, a bit rectified.

    words.forEach(function(word) {
        if (!discard.test(word)) {
            word = word.replace(punctuation, "");
            if (!stopWords.test(word.toLowerCase()) {
                word = word.substr(0, maxLength);
                tag_index[word.toLowerCase()] = word;
                word = word.toLowerCase();
                tags[word] = (tags[word] || 0) + 1);
            }
        }
    });
    

    It's a short writing for

    if (!condition) {
    }