pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith

/>

Esta página foi traduzida do inglês pela comunidade. Saiba mais e junte-se à comunidade MDN Web Docs.

View in English Always switch to English

String.prototype.startsWith()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since setembro de 2015.

O método startsWith() determina se uma string começa com os caracteres especificados, retornando true ou false.

Experimente

const str1 = "Saturday night plans";

console.log(str1.startsWith("Sat"));
// Expected output: true

console.log(str1.startsWith("Sat", 3));
// Expected output: false

Sintaxe

str.startsWith(searchString[, position])

Parâmetros

searchString

Os caracteres a serem procurados a partir do início dessa string.

position

Opcional. A posição nessa string na qual se inicia a busca pela searchString. O valor padrão é 0.

Valor retornado

true se os caracteres fornecidos forem encontrados no início da string. Se não, false.

Descrição

Esse método permite determinar se uma string começa ou não com outra string. Esse método é case-sensitive (difere maiúsculas de minúsculas, e vice-versa).

Exemplos

Usando startsWith()

js
//startswith
let str = "Ser ou não ser, eis a questão.";

console.log(str.startsWith("Ser")); // true
console.log(str.startsWith("não ser")); // false
console.log(str.startsWith("não ser", 7)); // true

Polyfill

Este método foi adicionaldo à especificação ECMAScript 2015 e pode ainda não estar disponível em todas as implementações do JavaScript. No entanto, você pode usar o polyfill String.prototype.startsWith() adicionando o seguinte código:

if (!String.prototype.startsWith) {
    Object.defineProperty(String.prototype, 'startsWith', {
        value: function(search, rawPos) {
            var pos = rawPos > 0 ? rawPos|0 : 0;
            return this.substring(pos, pos + search.length) === search;
        }
    });
}

Um polyfill mais robusto (totalmente conforme com a especificação ES2015), mas com menor desempenho e compacto está disponível no GitHub por Mathias Bynens.

Especificações

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.startswith

Veja também

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy