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

189
Visualizações
Why doesn't 'in' works for checking if a specific word is in an array in JavaScript

So, basically I'm working with string manipulation for a chatbot. When it returns a string from a message, I'm just checking if there's an specific word on it:

let msg = 'how are you?'                     //Message example
let words = msg.split(' ')                   //Words spliting
if ('are' in words) {}                       //This is where I'm having the problem

I know that in works for when I check a number inside of an array, but it doesn't seem to work with strings. Is there a different way to call it when it's about strings? I could do the checking using a loop, then a if (words[i] === 'are') {}, but I'd like to avoid that, if possible.

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

0

You can use includes:

if(words.includes('are')) { }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use words.includes("are"). Check the live demo:

let msg = 'how are you?'                     //Message example
let words = msg.split(' ')                   //Words spliting
console.log(words);
if (words.includes("are")) {
   console.log('exist');
} else {
   console.log('not exist');
}

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