Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!