Esta tradução está incompleta. Por favor, ajude a traduzir este artigo do Inglês.

O objeto Boolean é um wrapper objeto para um valor booliano.

Sintaxe

new Boolean([value])

Parâmetros

value
Opcional. O valor incial do objeto Boolean.

Descrição

The value passed as the first parameter is converted to a boolean value, if necessary. If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. If the DOM object document.all is passed as a parameter, the new boolean object also has an initial value of false. All other values, including any object or the string "false", create an object with an initial value of true.

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

Any object of which the value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:

var x = new Boolean(false);
if (x) {
  // this code is executed
}

This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to false:

var x = false;
if (x) {
  // this code is not executed
}

Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:

var x = Boolean(expression);     // preferred
var x = new Boolean(expression); // don't use

If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.

var myFalse = Boolean(false);   // initial value of false
var g = Boolean(myFalse);       // initial value of true
var myString = new String('Hello'); // string object
var s = Boolean(myString);      // initial value of true

Do not use a Boolean object in place of a Boolean primitive.

Propriedades

Boolean.length
Length property whose value is 1.
Boolean.prototype
Represents the prototype for the Boolean constructor.

Métodos

While the global Boolean object contains no methods of its own, it does inherit some methods through the prototype chain:

Instâncias Boolean

All Boolean instances inherit from Boolean.prototype. As with all constructors, the prototype object dictates instances' inherited properties and methods.

Propridades

Boolean.prototype.constructor
Returns the function that created an instance's prototype. This is the Boolean function by default.

Métodos

Boolean.prototype.toSource()
Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object. Overrides the Object.prototype.toSource() method.
Boolean.prototype.toString()
Returns a string of either "true" or "false" depending upon the value of the object. Overrides the Object.prototype.toString() method.
Boolean.prototype.valueOf()
Returns the primitive value of the Boolean object. Overrides the Object.prototype.valueOf() method.

Exemplos

Criar objetos Boolean com um valor inicial de false

var bNoParam = Boolean();
var bZero = Boolean(0);
var bNull = Boolean(null);
var bEmptyString = Boolean('');
var bfalse = Boolean(false);

Criar objetos Boolean com um valor inicial de verdadeiro

var btrue = Boolean(true);
var btrueString = Boolean('true');
var bfalseString = Boolean('false');
var bSuLin = Boolean('Su Lin');
var bArrayProto = Boolean([]);
var bObjProto = Boolean({});

Especificações

Especificação Estado Comentário
ECMAScript 1st Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.0.
ECMAScript 5.1 (ECMA-262)
The definition of 'Boolean' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Boolean' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'Boolean' in that specification.
Draft  

Compatibilidade de navegador

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
prototypeChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
toSource
Non-standard
Chrome No support NoEdge No support NoFirefox Full support 1IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android Full support 4Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
toStringChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
valueOfChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.

Consulte também

Etiquetas do documento e contribuidores

Contribuidores para esta página: mansil
Última atualização por: mansil,