Skip to main content

Null and Undefined

Null and Undefined are data types that handle cases where the value of an expression is not known.

Null

Null is a special data type with only one possible value: null. This value is returned by an expression that does not contain any value.

In the QodlyScript language and for object attribute properties, null values are managed through the null function. This function can be used with the following expressions for setting or comparing the null value:

  • object attributes
  • collection elements
  • variables of the object, collection, picture, or variant type.

null

null : null

ParameterTypeDescription
Resultnull<-Null value

Description

nullreturns the null type value null.

This function allows you to assign or compare the null value to the following language elements:

Language elementsComments
object property valuesComparing Null to an object property returns true if the property value is null, and false otherwise. To simplify code, comparing Null also returns true if the property does not exist in the object (i.e. is undefined), see example 4.
collection elementsWhen a collection is expanded by adding non-adjacent elements, any intermediary elements get automatically the null value.
object variablesAssigning the null value to such a variable type clears its contents. In this case, it has the same effect as calling the clearVariable command.
collection variablesAssigning the null value to such a variable type clears its contents. In this case, it has the same effect as calling the clearVariable command.
picture variablesAssigning the null value to such a variable type clears its contents. In this case, it has the same effect as calling the clearVariable command.
variant variables

Example 1

You assign and test the null value to an object property:

var vEmp : object
vEmp = newObject
vEmp.name = "Smith"
vEmp.children = null

if(vEmp.children == null) //true
end
if(vEmp.name == null) //false
end
if(vEmp.parent == null) //true
end

Example 2

You assign and compare the null value to a collection element:

var myCol : collection
myCol = newCollection(10,20,null)

if(myCol[2] == null)
// if the 3rd element is null
...
end

Example 3

These examples show the various ways to assign or compare the null value to variables:


//Object variable
var o : object
o = newObject
o = null //equivalent to clearVariable(o)
if(o != null) //equivalent to if(objectIsDefined(o))
end

//Collection variable
var c : collection
c = newCollection
c = null //equivalent to clearVariable(c)
if(c != null)
end

//Picture variable
var i : picture
i = vpicture
i = null //equivalent to clearVariable(i)
if(i != null) //equivalent to if(pictureSize(i) != 0)
end

Example 4

Here are the different results of the undefined command as well as the null command with object properties, depending on the context:

var vEmp : object
var vUndefined, vNull : boolean
vEmp = newObject
vEmp.name = "Smith"
vEmp.children = null

vUndefined = undefined(vEmp.name) // false
vNull = (vEmp.name == null) //false

vUndefined = undefined(vEmp.children) // false
vNull = (vEmp.children == null) //true

vUndefined = undefined(vEmp.parent) // true
vNull = (vEmp.parent == null) //true

Null operators

OperationSyntaxReturnsExpressionValue
Equalitynull == nullBooleana.nullProp == b.nullProptrue
null == undefinedBooleana.nullProp == b.undefinedProptrue
null == scalar valueBooleana.nullProp == 42false
Inequalitynull != nullBooleana.nullProp != b.nullPropfalse
null != undefinedBooleana.nullProp != b.undefinedPropfalse
null != scalar valueBooleana.nullProp != 42true

scalar values are values of type string, date, time, boolean, number, or blob. When declared, their default value is neither undefined nor null. Other types (picture, object, collection) have undefined or null default value. Ex:

var object : Object
var text : Text

//object == null
//text == ""
info

Comparisons with Greater than (>), Less than (<), Greater than or equal to (>=), and Less than or equal to (<=) operators are not supported with null values and return an error.

Undefined

Undefined is not actually a data type. It denotes a variable that has not yet been defined. A user function can return an undefined value if, within the method, the function result is assigned an undefined expression (an expression calculated with at least one undefined variable). An attribute cannot be undefined (the undefined command always returns false for an attribute). A variant variable has undefined as default value.

undefined

undefined ( expression : any )

ParameterTypeDescription
expressionany->Expression whose resulting value to be evaluated
Resultboolean<-true = undefined, false = not undefined

Description

undefinedreturns true if the result of the evaluation of expression is not a defined value, and false otherwise.

  • A variable is defined if it has been declared or if a value is assigned to it. It is undefined in all other cases.
  • An object property is undefined if it does not exist in the object.
  • A collection item is undefined if it does not exist in the collection.
note

Keep in mind that undefined evaluates expression. The following statements are equivalent:

var result : boolean
result = undefined(exp)
// same result:
result = (valueType(exp) == kUndefined)

Example

Here are the different results of the undefined command as well as the null command with object properties, depending on the context:

var vEmp : object
var vUndefined, vNull : boolean

vEmp = newObject
vEmp.name = "Smith"
vEmp.children = null

vUndefined = undefined(vEmp.name) // false
vNull = (vEmp.name == null) //false

vUndefined = undefined(vEmp.children) // false
vNull = (vEmp.children == null) //true

vUndefined = undefined(vEmp.parent) // true
vNull = (vEmp.parent == null) //true

Undefined operators

OperationSyntaxReturnsExpressionValue
Equalityundefined == undefinedBooleana.undefinedProp == b.undefinedProptrue
undefined == nullBooleana.undefinedProp == c.nullProptrue
undefined == other valuesBooleana.undefinedProp == 42false
Inequalityundefined != undefinedBooleana.undefinedProp != b.undefinedPropfalse
undefined != nullBooleana.undefinedProp != b.nullPropfalse
undefined != other valuesBooleana.undefinedProp != 42true
Greater thanundefined > string, date, time, boolean, numberBooleana.undefinedProp > "abc"false
Less thanundefined < string, date, time, boolean, numberBooleana.undefinedProp < "abc"false
Greater than or equal toundefined >= string, date, time, boolean, numberBooleana.undefinedProp >= "abc"false
Less than or equal toundefined <= string, date, time, boolean, numberBooleana.undefinedProp <= "abc"false

other values are expressions of any type with a value neither undefined nor null.

info

Comparisons of undefined values with picture, blob, object, collection, undefined or null values using Greater than (>), Less than (<), Greater than or equal to (>=), and Less than or equal to (<=) operators are not supported and return an error.