blob: a962d7771a21fc504d0456d311826fda2a974fb6 (
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
|
---
title: Boolean.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/Boolean/toString
tags:
- Boolean
- JavaScript
- Méthode
- Prototype
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Boolean/toString
original_slug: Web/JavaScript/Reference/Objets_globaux/Boolean/toString
---
{{JSRef}}
La méthode **`toString()`** renvoie une chaine de caractères correspondant à l'objet `Boolean` courant.
{{EmbedInteractiveExample("pages/js/boolean-tostring.html")}}
## Syntaxe
bool.toString()
### Valeur de retour
Une chaîne de caractères qui représente l'objet {{jsxref("Boolean")}}.
## Description
L'objet {{jsxref("Boolean")}} surcharge la méthode `toString()` de l'objet {{jsxref("Object")}} ; il n'hérite pas de la méthode {{jsxref("Object.prototype.toString()")}}. Pour les objets de type `Boolean`, la méthode `toString()` renvoie une chaine de caractère representative de l'objet.
La méthode `toString()` est automatiquement appelée quand le `Boolean` doit être representé comme une texte ou lorsque qu'il est concaténé avec une chaine de caractères.
Pour les objets de type `Boolean` , la fonction native `toString()` renvoie la chaine de caractère "`true`" ou "`false`" en fonction de la valeur de l'objet.
## Exemples
### Utiliser `toString()`
Dans ce code, `flag.toString()` renvoie "`true`" :
```js
var flag = new Boolean(true);
var maVar = flag.toString();
```
## Spécifications
| Spécification | Statut | Commentaires |
| -------------------------------------------------------------------------------------------------------------------- | ---------------------------- | ----------------------------------------------------- |
| {{SpecName('ES1')}} | {{Spec2('ES1')}} | Définition initiale. Implémentée avec JavaScript 1.1. |
| {{SpecName('ES5.1', '#sec-15.6.4.2', 'Boolean.prototype.toString')}} | {{Spec2('ES5.1')}} | |
| {{SpecName('ES6', '#sec-boolean.prototype.tostring', 'Boolean.prototype.toString')}} | {{Spec2('ES6')}} | |
| {{SpecName('ESDraft', '#sec-boolean.prototype.tostring', 'Boolean.prototype.toString')}} | {{Spec2('ESDraft')}} | |
## Compatibilité des navigateurs
{{Compat("javascript.builtins.Boolean.toString")}}
## Voir aussi
- {{jsxref("Object.prototype.toString()")}}
|