Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

374
Visualizações
(JS) Basic question- return in if statement

I'm new for learning Javascript, and have encountered a problem that I cannot figure out it's rationale in an online course. Please take a look on the code below.

function palindrome(word){ 
  let backwardWord = "";
  for (letter of word){
    // Adding letter at the beginning of the backward string
    backwardWord = letter + backwardWord;
}
  if (word.toLowerCase() === backwardWord.toLowerCase())
    return true
  else
    return false  
}
palindrome('racecar');

As you may know, this function aims to find if the string reads the same in forward or backward.

My question is, for what I know, if statement requires {} in order to run, and return can only be used in function, not a if statement. Then why there are no {} in the if statement, and why a return could be used in the code above?

I've tried to rewrite a function using the same format, but it returns undefined rather than true.

let a=1
function test(){
if (a===1)
    return true
  else
    return false  
}

Thanks for anyone who answer this question, that will help a lot as I was puzzling for the past 0.5 hour and can't .

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

First of all the second function is running and returning true

let a=1
function test(){
if (a===1)
    return true
  else
    return false  
}

console.log(test());
console.log(typeof(test()));

If you have 1 line code after if, {} are not necessary.

And here is your first code. Better use for (let letter of word)

function palindrome(word){ 
    let backwardWord = "";
    for (let letter of word){
      backwardWord = letter + backwardWord;
  }
    if (word.toLowerCase() === backwardWord.toLowerCase()){
        console.log(backwardWord);
        return true;
    }
    else
      return false  
  }

console.log(palindrome('racecar'));

And last, return statement will get you out of the function immediately. You are returning true which has a type boolean, you can see using typeof() function in the second code.

about 4 years ago · Juan Pablo Isaza Relatório

0

When there is only one line inside a block then {} can be omitted. for example:

for(int i=0;i<5;i++){
 print(i)
}

can be written as:

for(int i=0;i<5;i++)
 print(i)

As for your second option, undefined is shown because you have defined your function, but never called it or used it. If you type

test()

in the next line of your code then you will get true as a answer.

about 4 years ago · Juan Pablo Isaza Relatório

0

You are allowed to return from anywhere within a function. return, much like break, immediately leaves whatever block it is executing.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda