JS_ValueToBoolean

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_ValueToBoolean
  • Revision title: JS_ValueToBoolean
  • Revision id: 146662
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment /* Description */ minor copy-editing

Revision Content

{{template.Jsapi_ref_header("JS_ValueToBoolean")}}

Convert any JavaScript value to a boolean value.

Syntax

JSBool JS_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. {{wiki.template('Jsapi-requires-request')}}
v jsval The JS value to convert.
bp JSBool * Out parameter. On success, *bp receives the converted boolean value.

Description

JS_ValueToBoolean converts a specified JavaScript value, v, to a boolean value. It implements the ToBoolean operator specified in {{template.Es3_spec(9.2)}}. On success, JS_ValueToBoolean stores the converted value in *bp and returns JS_TRUE. On error or exception, it returns JS_FALSE, and the value left in *bp is undefined.

For versions greater than JSVERSION_1_2, all objects convert to JS_TRUE, even Boolean wrapper objects like the one produced by new Boolean(false). This is the behavior specified in {{template.Es3_spec(9.2)}}. See related discussion on Google Groups.

{{template.LXRSearch("ident", "i", "JS_ValueToBoolean")}}

Revision Source

<p>{{template.Jsapi_ref_header("JS_ValueToBoolean")}}
</p><p>Convert any JavaScript value to a boolean value.
</p>
<h2 name="Syntax"> Syntax </h2>
<pre class="eval"><a href="en/JSBool">JSBool</a> <b>JS_ValueToBoolean</b>(<a href="en/JSContext">JSContext</a> *cx, <a href="en/Jsval">jsval</a> v, <a href="en/JSBool">JSBool</a> *bp);
</pre>
<table class="fullwidth-table">
<tbody><tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td><code>cx</code></td>
<td><code><a href="en/JSContext">JSContext</a> *</code></td>
<td>Pointer to a JS context from which to derive runtime information.
{{wiki.template('Jsapi-requires-request')}}</td>
</tr>
<tr>
<td><code>v</code></td>
<td><code><a href="en/Jsval">jsval</a></code></td>
<td>The JS value to convert.</td>
</tr>
<tr>
<td><code>bp</code></td>
<td><code><a href="en/JSBool">JSBool</a> *</code></td>
<td>Out parameter.  On success, <code>*bp</code> receives the converted boolean value.</td>
</tr>
</tbody></table>
<h2 name="Description"> Description </h2>
<p><code>JS_ValueToBoolean</code> converts a specified JavaScript value, <code>v</code>, to a boolean value.  It implements the ToBoolean operator specified in {{template.Es3_spec(9.2)}}.  On success, <code>JS_ValueToBoolean</code> stores the converted value in <code>*bp</code> and returns <code>JS_TRUE</code>.   On error or exception, it returns <code>JS_FALSE</code>, and the value left in <code>*bp</code> is undefined. 
</p><p>For versions greater than <code>JSVERSION_1_2</code>, all objects convert to <code>JS_TRUE</code>, even <code>Boolean</code> wrapper objects like the one produced by <code>new Boolean(false)</code>.  This is the behavior specified in {{template.Es3_spec(9.2)}}.  See related discussion on <a class="external" href="http://groups.google.com/group/netscape.public.mozilla.jseng/browse_thread/thread/13de1825f1935156/5c1901b7d4b31c4f">Google Groups</a>.
</p><p>{{template.LXRSearch("ident", "i", "JS_ValueToBoolean")}}
</p>
Revert to this revision