blob: 7c6803b778d71aa0fba5ecfeea2c67e0c11c17a2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
---
title: String.prototype.startsWith()
slug: Web/JavaScript/Reference/Global_Objects/String/startsWith
tags:
- ECMAScript6
- JavaScript
- Méthode
- Prototype
- Reference
- String
- polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/String/startsWith
original_slug: Web/JavaScript/Reference/Objets_globaux/String/startsWith
---
{{JSRef}}
La méthode **`startsWith()`** renvoie un booléen indiquant si la chaine de caractères commence par la deuxième chaine de caractères fournie en argument.
{{EmbedInteractiveExample("pages/js/string-startswith.html")}}
## Syntaxe
str.startsWith(chaîneRecherchée [, position]);
### Paramètres
- `chaîneRecherchée`
- : Les caractères à rechercher au début de la chaine de caractères.
- `position` {{optional_inline}}
- : La position à laquelle commencer la recherche de `chaîneRecherchée` ; par défaut 0.
### Valeur de retour
`true` si la chaîne de caractères commence avec la sous-chaîne en argument, `false` sinon
## Description
Cette méthode permet de savoir si une chaine de caractères commence avec une autre chaine de caractères (comme pour les autres méthodes fonctionnant avec les chaînes de caractères, cette méthode est sensible à la casse).
## Exemples
```js
var str = "Être, ou ne pas être : telle est la question.";
console.log(str.startsWith("Être")); // true
console.log(str.startsWith("pas être")); // false
console.log(str.startsWith("pas être", 12)); // true
```
## Spécifications
| Spécification | État | Commentaires |
| ------------------------------------------------------------------------------------------------------------------------ | ---------------------------- | -------------------- |
| {{SpecName('ES2015', '#sec-string.prototype.startswith', 'String.prototype.startsWith')}} | {{Spec2('ES2015')}} | Définition initiale. |
| {{SpecName('ESDraft', '#sec-string.prototype.startswith', 'String.prototype.startsWith')}} | {{Spec2('ESDraft')}} | |
## Compatibilité des navigateurs
{{Compat("javascript.builtins.String.startsWith")}}
## Voir aussi
- {{jsxref("String.prototype.endsWith()")}}
- {{jsxref("String.prototype.includes()")}}
- {{jsxref("String.prototype.indexOf()")}}
- {{jsxref("String.prototype.lastIndexOf()")}}
- [Prothèse (_polyfill_) de Mathias Bynens](https://github.com/mathiasbynens/String.prototype.startsWith)
|