Skip to content

Commit

Permalink
Move emphasis from "Any" to "object" (#32018)
Browse files Browse the repository at this point in the history
* Move emphasis from "Any" to "object"

- My intent is to establish early that that sentence that follows pertains to Boolean objects, not primitives.
- Whether this is an improvement is highly subjective, but given the section is titled "Boolean primitives and Boolean objects," it seemed to me that the word "object" should stand out more than the word "Any."

* Rewrote "Boolean primitives and Boolean objects"

- On my first attempt I didn't truly realize what the line I changed was articulating until I received feedback. Here is my second attempt at this PR.
- This change may clash with the tone of the rest of the article because it is written in a more casual voice, but if that's an issue, I'd be happy to change that if there's potential behind the edit.
- If this is not an improvement, I'm okay with closing this PR without a merge.

* Update files/en-us/web/javascript/reference/global_objects/boolean/index.md

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Better wording

* Update files/en-us/web/javascript/reference/global_objects/boolean/index.md

Co-authored-by: Dipika Bhattacharya <dipika@foss-community.org>

* Update index.md

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
Co-authored-by: Dipika Bhattacharya <dipika@foss-community.org>
  • Loading branch information
4 people committed Feb 12, 2024
1 parent e36ec8f commit ef3d04e
Showing 1 changed file with 17 additions and 26 deletions.
Expand Up @@ -13,41 +13,32 @@ The **`Boolean`** object represents a truth value: `true` or `false`.

### Boolean primitives and Boolean objects

Do not confuse the {{Glossary("Primitive", "primitive")}} `Boolean` values `true` and `false` with the `true` and `false` values of the `Boolean` object.
For converting non-boolean values to boolean, use `Boolean` as a function or use the [double NOT](/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#double_not_!!) operator. Do not use the `Boolean()` constructor with `new`.

**Any** object, including a `Boolean` object whose value is `false`, evaluates to `true` when passed to a conditional statement. For example, the condition in the following {{jsxref("Statements/if...else", "if")}} statement evaluates to `true`:

```js
const x = new Boolean(false);
if (x) {
// this code is executed
}
```js example-good
const good = Boolean(expression);
const good2 = !!expression;
```

This behavior does not apply to `Boolean` primitives. For example, the condition in the following {{jsxref("Statements/if...else", "if")}} statement evaluates to `false`:

```js
const x = false;
if (x) {
// this code is not executed
}
```js example-bad
const bad = new Boolean(expression); // don't use this!
```

Do not use the `Boolean()` constructor with `new` to convert a non-boolean value to a boolean value — use `Boolean` as a function or a [double NOT](/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#double_not_!!) instead:
This is because _all_ objects, including a `Boolean` object whose wrapped value is `false`, are {{glossary("truthy")}} and evaluate to `true` in places such as conditional statements. (See also the [boolean coercion](#boolean_coercion) section below.)

```js
const good = Boolean(expression); // use this
const good2 = !!expression; // or this
const bad = new Boolean(expression); // don't use this!
```
if (new Boolean(true)) {
console.log("This log is printed.");
}

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`.
if (new Boolean(false)) {
console.log("This log is ALSO printed.");
}

```js
const myFalse = new Boolean(false); // initial value of false
const g = Boolean(myFalse); // initial value of true
const myString = new String("Hello"); // string object
const s = Boolean(myString); // initial value of true
const myFalse = new Boolean(false); // myFalse is a Boolean object (not the primitive value false)
const g = Boolean(myFalse); // g is true
const myString = new String("Hello"); // myString is a String object
const s = Boolean(myString); // s is true
```

> **Warning:** You should rarely find yourself using `Boolean` as a constructor.
Expand Down

0 comments on commit ef3d04e

Please sign in to comment.