Several operators can be applied to objects of any type.


as()

The as() operators coerces objects of one type to be an object of another. The target type is given as a string argument to the operator. The following types are available:

The following coercions are valid:

An error is thrown for other attempted coercions, unless the value being coerced by as() is already of the target type.

For example, the following constructs a string and then turns it into a regular expression that can be used by the replace() operator:

("re#q=" + q + "#i").as("RegExp")

defaultsTo()

The defaultsTo() operator returns the value of its first argument if the object is null. The second argument is a string and is optional. If present, it is written in the log if the object is null. 

doesnotexist.defaultsTo(x);  // returns the value of x
doesnotexist.defaultsTo([]); // returns the empty array

The operator is most often used to guard against null values.

isnull()

The isnull() operator returns true of the object is null (undefined) and false otherwise.


doesnotexist.isnull(); // returns true


klog()

The klog() operator writes an optional message, given as a string argument, and the value of the object to the debug log. 

x = some_arr.filter(function(x){x == 5}).klog("Value after filter: ").head();

typeof()

The typeof() operator returns the type of the object to which it is applied. The return value is one of the strings that may be passed to as(), or one of the other following values:

For example:

nums = [1, 2, 3]
nums_type = nums.typeof() // nums_type = "Array"