Use the nz function to return a zero-length string (" "), or another specified value when a variant is null.

Syntax

JSV_nz(variant [, valueifnull])

Prototype

n/a

Returns

If the value of variant is null, the nz function returns a zero-length string. If the optional argument valueifnull is included, then the nz function will return the value specified by that argument if variant is null.

If the value of variant is not null, then the nz function returns the value of variant.

Parameters

The nz function uses the following arguments.

Argument Description
variant A variable of data type Variant.
valueifnull Optional. This argument supplies a value to be returned if the variant argument is null. This argument enables you to return a value other than a zero-length string. Default value is a zero-length string.

Remarks

The nz function is useful for expressions that may include null values. To force an expression to evaluate to a non-null value even when it contains a null value, use the nz function to return a zero-length string, or a custom return value.

You can often use the nz function as an alternative to the conditional operator. For example, in the following code, two expressions including the conditional operator are necessary to return the desired result. The first expression including the conditional operator is used to check the value of a variable and convert it to zero if it is null.

varTemp = (varFreight == null) ? 0 : varFreight;
varResult = (varTemp > 50) ? "High" : "Low";

In the next example, the nz function provides the same functionality as the first expression, and the desired result is achieved in one step rather than two.

varResult = JSV_nz(varFreight) > 50 ? "High" : "Low";

If you supply a value for the optional argument valueifnull, that value will be returned when variant is null. By including this optional argument, you may be able to avoid using an expression containing the conditional operator.

Dependencies

JSV_isEmpty

Example

The following lines of code

var f = '';
document.write( "The value of f is " + JSV_nz(f,43) );
f = null;
document.write( "The value of f is " + JSV_nz(f,"excellent") );

produce