Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Boolean

BaselineWidely available

Boolean values can be one of two values:true orfalse, representing the truth value of a logical proposition.

Description

Boolean values are typically produced byrelational operators,equality operators, andlogical NOT (!). They can also be produced by functions that represent conditions, such asArray.isArray(). Note thatbinary logical operators such as&& and|| return the values of the operands, which may or may not be boolean values.

Boolean values are typically used in conditional testing, such as the condition forif...else andwhile statements, theconditional operator (? :), or the predicate return value ofArray.prototype.filter().

You would rarely need to explicitly convert something to a boolean value, as JavaScript does this automatically in boolean contexts, so you can use any value as if it's a boolean, based on itstruthiness. You are also encouraged to useif (condition) andif (!condition) instead ofif (condition === true) orif (condition === false) in your own code so you can take advantage of this convention. However, making sure that values representing conditions are always booleans can help clarify the intent of your code.

js
// Do this:// This always returns a boolean valueconst isObject = (obj) => !!obj && typeof obj === "object";// Or this:const isObject = (obj) => Boolean(obj) && typeof obj === "object";// Or this:const isObject = (obj) => obj !== null && typeof obj === "object";// Instead of this:// This may return falsy values that are not equal to falseconst isObject = (obj) => obj && typeof obj === "object";

Boolean primitives and Boolean objects

For converting non-boolean values to boolean, useBoolean as a function or use thedouble NOT operator. Do not use theBoolean() constructor withnew.

js
const good = Boolean(expression);const good2 = !!expression;
js
const bad = new Boolean(expression); // don't use this!

This is becauseall objects, including aBoolean object whose wrapped value isfalse, aretruthy and evaluate totrue in places such as conditional statements. (See also theboolean coercion section below.)

js
if (new Boolean(true)) {  console.log("This log is printed.");}if (new Boolean(false)) {  console.log("This log is ALSO printed.");}const myFalse = new Boolean(false); // myFalse is a Boolean object (not the primitive value false)const g = Boolean(myFalse); // g is trueconst myString = new String("Hello"); // myString is a String objectconst s = Boolean(myString); // s is true

Warning:You should rarely find yourself usingBoolean as a constructor.

Boolean coercion

Many built-in operations that expect booleans first coerce their arguments to booleans.The operation can be summarized as follows:

  • Booleans are returned as-is.
  • undefined turns intofalse.
  • null turns intofalse.
  • 0,-0, andNaN turn intofalse; other numbers turn intotrue.
  • 0n turns intofalse; otherBigInts turn intotrue.
  • The empty string"" turns intofalse; other strings turn intotrue.
  • Symbols turn intotrue.
  • All objects becometrue.

Note:A legacy behavior makesdocument.all returnfalse when used as a boolean, despite it being an object. This property is legacy and non-standard and should not be used.

Note:Unlike other type conversions likestring coercion ornumber coercion, boolean coercion does not attempt toconvert objects to primitives by calling user methods.

In other words, there are only a handful of values that get coerced tofalse — these are calledfalsy values. All other values are calledtruthy values. A value's truthiness is important when used with logical operators, conditional statements, or any boolean context.

There are two ways to achieve the same effect in JavaScript.

  • Double NOT:!!x negatesx twice, which convertsx to a boolean using the same algorithm as above.
  • TheBoolean() function:Boolean(x) uses the same algorithm as above to convertx.

Note that truthiness is not the same as beingloosely equal totrue orfalse.

js
if ([]) {  console.log("[] is truthy");}if ([] == false) {  console.log("[] == false");}// [] is truthy// [] == false

[] is truthy, but it's also loosely equal tofalse. It's truthy, because all objects are truthy. However, when comparing withfalse, which is a primitive,[] is also converted to a primitive, which is"" viaArray.prototype.toString(). Comparing strings and booleans results in both beingconverted to numbers, and they both become0, so[] == false istrue. In general, falsiness and== false differ in the following cases:

  • NaN,undefined, andnull are falsy but not loosely equal tofalse.
  • "0" (and other string literals that are not"" butget coerced to 0) is truthy but loosely equal tofalse.
  • Objects are always truthy, but their primitive representation may be loosely equal tofalse.

Truthy values are even more unlikely to be loosely equal totrue. All values are either truthy or falsy, but most values are loosely equal to neithertrue norfalse.

Constructor

Boolean()

CreatesBoolean objects. When called as a function, it returns primitive values of type Boolean.

Instance properties

These properties are defined onBoolean.prototype and shared by allBoolean instances.

Boolean.prototype.constructor

The constructor function that created the instance object. ForBoolean instances, the initial value is theBoolean constructor.

Instance methods

Boolean.prototype.toString()

Returns a string of eithertrue orfalse depending upon the value of the object. Overrides theObject.prototype.toString() method.

Boolean.prototype.valueOf()

Returns the primitive value of theBoolean object. Overrides theObject.prototype.valueOf() method.

Examples

Creating false values

js
const bNoParam = Boolean();const bZero = Boolean(0);const bNull = Boolean(null);const bEmptyString = Boolean("");const bfalse = Boolean(false);

Creating true values

js
const btrue = Boolean(true);const btrueString = Boolean("true");const bfalseString = Boolean("false");const bSuLin = Boolean("Su Lin");const bArrayProto = Boolean([]);const bObjProto = Boolean({});

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-boolean-objects

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp