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

129
Visualizações
Why do String.includes acts different with space characters?

So, I have this method to check if an object has a specific status:

export const verifyStatus = (productStatus: string, expectedStatus: string): boolean => {
  return productStatus.split(' ').some((status) => expectedStatus.includes(status));
};

I'll use this as my values:

const productStatus = 'POTATO BANANA TOMATO LETTUCE'
const expectedStatus = 'EGG'

The weird thing is, this is false:

'POTATO BANANA TOMATO LETTUCE'
    .split(' ')
    .some((status) => 'EGG'.includes(status));

This is true (notice the double space between POTATO and BANANA)

'POTATO  BANANA TOMATO LETTUCE'
    .split(' ')
    .some((status) => 'EGG'.includes(status));

But this is working despite any double space (true if I add EGG to my first string and false if I remove):

'POTATO BANANA TOMATO LETTUCE'
    .split(' ')
    .some((status) => status.includes('EGG'));
'POTATO  BANANA TOMATO LETTUCE'
    .split(' ')
    .some((status) => status.includes('EGG'));

Why is that happening? Am I missing something? I had this method for about 5 months and it was working perfectly until a few hours ago.

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

0

"EGG".includes("") is true. "".includes("EGG") is false. That's why you get different results from the two versions of your code.

Why ""? Because .split(" ") on a string containing two spaces in a row will put a blank string in the array:

console.log("TEST  ING".split(" "));

You can tell split to split on any sequence of spaces using a regular expression -- .split(/ +/) for just spaces, or .split(/\s+/) for runs of whitespace:

console.log(
    "POTATO  BANANA TOMATO LETTUCE"
        .split(/\s+/)
        .some((status) => "EGG".includes(status))
);

about 4 years ago · Juan Pablo Isaza Relatório

0

(status) => 'EGG'.includes(status) is asking whether the value of status is found somewhere in the string 'EGG'. The empty string '' resulting from the double space is obviously found there in several places.

If you just want to check whether EGG is among the space-separated items, you should use Array.includes:

'POTATO  BANANA TOMATO LETTUCE'
    .split(' ')
    .includes('EGG')
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