aboutsummaryrefslogtreecommitdiff
path: root/files/pt-br/web/javascript/reference/global_objects/boolean/index.html
blob: 92814d5671a3e970dfcc71236d96ae1a71ef3521 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
title: Boolean
slug: Web/JavaScript/Reference/Global_Objects/Boolean
tags:
  - Boolean
  - Constructor
  - JavaScript
translation_of: Web/JavaScript/Reference/Global_Objects/Boolean
---
<div>{{JSRef}}</div>

<p>O objeto <strong><code>Boolean</code></strong> é um objeto wrapper para um valor booleano.</p>

<h2 id="Sintaxe">Sintaxe</h2>

<pre class="syntaxbox"><code>new Boolean([<var>value</var>])</code></pre>

<h3 id="Parâmetros">Parâmetros</h3>

<dl>
 <dt><code>value</code></dt>
 <dd>Opcional. O valor inicial do objeto <code>Boolean.</code></dd>
</dl>

<h2 id="Descrição">Descrição</h2>

<p>O valor passado como primeiro parâmetro é convertido para um valor boleano, se necessário. Se o valor é omitido ou é <code>0</code>, <code>-0</code>, {{jsxref("null")}}, <code>false</code>, {{jsxref("NaN")}}, {{jsxref("undefined")}} ou é uma string vazia(<code>""</code>), o objeto terá um valor inicial de <code>false</code>. Todos outros valores, incluindo qualquer objeto ou string <code>"false"</code>,  criam um objeto com valor inicial  <code>true</code>.</p>

<p>Não confunda os valores primitivos Boolean <code>true</code><code>false</code> com os valores <code>true</code> and <code>false</code> do objeto <code>Boolean</code>.</p>

<p>Qualquer objeto cujo o valor não é {{jsxref("undefined")}} ou {{jsxref("null")}}, incluindo um objeto <code>Boolean</code> que o valor seja <code>false</code>, é avaliado para <code>true</code> quando passa por uma declaração condicional. Por exemplo, a condição a seguir {{jsxref("Statements/if...else", "if")}} a declaração é avaliada como <code>true</code>:</p>

<pre class="brush: js">var x = new Boolean(false);
if (x) {
  // esse código é executado
}
</pre>

<p>Esse comportamento não se aplica aos primitivos <code>Boolean</code>. Por exemplo, a condição a seguir {{jsxref("Statements/if...else", "if")}} a declaração é avaliada como <code>false</code>:</p>

<pre class="brush: js">var x = false;
if (x) {
  // esse código não é executado
}
</pre>

<p>Não use um objeto <code>Boolean</code> para converter um valor não-boleano para um valor boleano. Ao invés disso use <code>Boolean</code> como uma função para executar essa tarefa:</p>

<pre class="brush: js">var x = Boolean(expression);     // preferido
var x = new Boolean(expression); // não use
</pre>

<p>Se você especificar qualquer objeto, incluindo um objeto <code>Boolean</code> cujo valor é <code>false</code>, como valor inicial de um objeto <code>Boolean</code>, o novo objeto <code>Boolean</code> terá o valor de <code>true</code>.</p>

<pre class="brush: js">var myFalse = new Boolean(false);   // valor inicial false
var g = new Boolean(myFalse);       // valor inicial true
var myString = new String('Hello'); // objeto String
var s = new Boolean(myString);      // valor inicial true
</pre>

<p>Não use um um objeto <code>Boolean</code> no lugar de um primitivo B<code>oolean</code>.</p>

<h2 id="Propriedades">Propriedades</h2>

<dl>
 <dt><code>Boolean.length</code></dt>
 <dd>Propriedade Length cujo valor é 1.</dd>
 <dt>{{jsxref("Boolean.prototype")}}</dt>
 <dd>Representa o protótipo para o construtor <code>Boolean</code>.</dd>
</dl>

<h2 id="Métodos">Métodos</h2>

<p>O objeto global <code>Boolean</code> contém métodos próprios, entretanto,  ele herda alguns métodos através da cadeia de protótipos:</p>

<h2 id="Instâncias_Boolean">Instâncias <code>Boolean</code></h2>

<p>Todas instâncias <code>Boolean</code> herdam de {{jsxref("Boolean.prototype")}}. Assim como todos os construtores, o protótipo do objeto dita as propriedades e métodos herdados.</p>

<h3 id="Propriedades_2">Propriedades</h3>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/prototype', 'Properties')}}</div>

<h3 id="Métodos_2">Métodos</h3>

<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/prototype', 'Methods')}}</div>

<h2 id="Exemplos">Exemplos</h2>

<h3 id="Criando_objetos_Boolean_com_um_valor_inicial_false">Criando objetos <code>Boolean</code> com um valor inicial <code>false</code></h3>

<pre class="brush: js">var bNoParam = new Boolean();
var bZero = new Boolean(0);
var bNull = new Boolean(null);
var bEmptyString = new Boolean('');
var bfalse = new Boolean(false);
</pre>

<h3 id="Criando_objetos_Boolean_com_um_valor_inicial_true">Criando objetos <code>Boolean</code> com um valor inicial <code>true</code></h3>

<pre class="brush: js">var btrue = new Boolean(true);
var btrueString = new Boolean('true');
var bfalseString = new Boolean('false');
var bSuLin = new Boolean('Su Lin');
var bArrayProto = new Boolean([]);
var bObjProto = new Boolean({});
</pre>

<h2 id="Especificações">Especificações</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Especificação</th>
   <th scope="col">Status</th>
   <th scope="col">Comentário</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Definição inicial. Implementado no Java Script 1.0.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.6', 'Boolean')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-boolean-objects', 'Boolean')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Compatibilidade com navegadores</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Suporte básico</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatIE("6.0")}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Suporte básico</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Veja_também">Veja também</h2>

<ul>
 <li>{{jsxref("Boolean.prototype")}}</li>
 <li>{{Glossary("Boolean")}}</li>
 <li><a href="http://en.wikipedia.org/wiki/Boolean_data_type">Boolean data type (Wikipedia)</a></li>
</ul>