JS_ValueToBoolean

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_ValueToBoolean
  • Revision title: JS_ValueToBoolean
  • Revision id: 146659
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment /* Notes */ link standard

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 JS value, v, to a boolean value. The actual conversion is performed by the object's convert operation. Converting any JS value to a Boolean always succeeds, except when the JS value is a JSVAL_OBJECT that does not support its own conversion routine.

The converted value is stored in the JSBool pointed to by bp. If the conversion is successful, JS_ValueToBoolean returns JS_TRUE. If the value to convert is an empty string, or conversion is unsuccessful, JS_ValueToBoolean returns JS_FALSE.

Notes

Calling JS_ValueToBoolean against a Boolean wrapper object always returns true for versions greater than JSVERSION_1_2. This duplicates the behavior of the ToBoolean operator as specified in ECMA 262-3 § 9.2. Related discussion on Google Groups.

See Also

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

JS_ConvertArguments, JS_ConvertValue, JS_GetTypeName, JS_TypeOfValue, JS_ValueToFunction, JS_ValueToInt32, JS_ValueToNumber, JS_ValueToObject, JS_ValueToString

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 JS value, <code>v</code>, to a boolean value. The actual conversion is performed by the object's convert operation. Converting any JS value to a <code>Boolean</code> always succeeds, except when the JS value is a <code>JSVAL_OBJECT</code> that does not support its own conversion routine.
</p><p>The converted value is stored in the <code>JSBool</code> pointed to by <code>bp</code>. If the conversion is successful, <code>JS_ValueToBoolean</code> returns <code>JS_TRUE</code>. If the value to convert is an empty string, or conversion is unsuccessful, <code>JS_ValueToBoolean</code> returns <code>JS_FALSE</code>.
</p>
<h2 name="Notes"> Notes </h2>
<p>Calling <code>JS_ValueToBoolean</code> against a <code>Boolean</code> wrapper object always returns true for versions greater than <code>JSVERSION_1_2</code>.  This duplicates the behavior of the ToBoolean operator as specified in <a class="external" href="http://bclary.com/2004/11/07/#a-9.2">ECMA 262-3 § 9.2</a>.  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>
<h2 name="See_Also"> See Also </h2>
<p>{{template.LXRSearch("ident", "i", "JS_ValueToBoolean")}}
</p><p><a href="en/JS_ConvertArguments">JS_ConvertArguments</a>,
<a href="en/JS_ConvertValue">JS_ConvertValue</a>,
<a href="en/JS_GetTypeName">JS_GetTypeName</a>,
<a href="en/JS_TypeOfValue">JS_TypeOfValue</a>,
<a href="en/JS_ValueToFunction">JS_ValueToFunction</a>,
<a href="en/JS_ValueToInt32">JS_ValueToInt32</a>,
<a href="en/JS_ValueToNumber">JS_ValueToNumber</a>,
<a href="en/JS_ValueToObject">JS_ValueToObject</a>,
<a href="en/JS_ValueToString">JS_ValueToString</a>
</p>
Revert to this revision