Javascript syntax - if statements and function calls

I have a syntax issue, let's say I have the following function. He received the text to check spelling and returned the correct text, or false if the text was already spelled correctly.

var spellCheck = function (txt) {
    var spelledTxt = spell(txt);
    if (spelledTxt =! txt) { //the text had a typo.
        return spelledTxt;
    }
    return false;
}

Now in another function I want to call this function from the ElseIf instruction, and I want to get into this instruction only if there was a typo in the text that was corrected. sort of:

if(something){
    do something....
}else if(spellCheck(word)){
    do something with the spelledTxt that was returned from "spellcheck"
}

Can I just do:

else if(var spelledWord = spellCheck(word))
    do something with spelledWord.

I forgot a very important thing: spellCheck(word)- a very heavy function, and I would like to avoid the call, if not needed. This means that only if we come to else if(), it will be called, not earlier.

+4
source share
2 answers

if . - .

, var , .

, .

:

else {
 var spelledWord = spellCheck(word);
 if(spelledWord)
    do something with spelledWord
}
+5

. , .

, :

  • Javascript var .
  • Javascript "".
  • 1,2 .
  • Javascript .
  • , - .

: if (spelledWord = spellCheck(word) !== '') , memoize () spellCheck. .

-1

Source: https://habr.com/ru/post/1617899/


All Articles