QJSPrimitiveValue Class | Qt Qml 6.8.3
Description: The QJSPrimitiveValue class operates on primitive types in JavaScript semantics.

- Qt 6.8
- Qt Qml
- C++ Classes
- QJSPrimitiveValue
- Qt 6.8.3 Reference Documentation

# QJSPrimitiveValue Class

The QJSPrimitiveValue class operates on primitive types in JavaScript semantics. More...

Header: | `#include <QJSPrimitiveValue>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Qml)` `target_link_libraries(mytarget PRIVATE Qt6::Qml)`
qmake: | `QT += qml`
Since: | Qt 6.1

- List of all members, including inherited members

## Public Types

enum | Type { Undefined, Null, Boolean, Integer, Double, String }

## Public Functions

| QJSPrimitiveValue()
| QJSPrimitiveValue(QJSPrimitiveNull null)
| QJSPrimitiveValue(QJSPrimitiveUndefined undefined)
| QJSPrimitiveValue(bool value)
| QJSPrimitiveValue(class QString value)
| QJSPrimitiveValue(const QVariant &value)
| QJSPrimitiveValue(double value)
| QJSPrimitiveValue(int value)
`(since 6.4)` | QJSPrimitiveValue(int type, const void *value)
`(since 6.6)` const void * | constData() const
`(since 6.6)` void * | data()
`(since 6.6)` const void * | data() const
bool | equals(const QJSPrimitiveValue &other) const
`(since 6.6)` int | metaType() const
bool | strictlyEquals(const QJSPrimitiveValue &other) const
`(since 6.6)` QJSPrimitiveValue | to() const
bool | toBoolean() const
double | toDouble() const
int | toInteger() const
int | toString() const
QJSPrimitiveValue::Type | type() const

## Related Non-Members

`(since 6.1)` bool | operator!=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` QJSPrimitiveValue | operator*(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` QJSPrimitiveValue | operator+(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` QJSPrimitiveValue | operator-(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` QJSPrimitiveValue | operator/(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` bool | operator<(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` bool | operator<=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` bool | operator==(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` bool | operator>(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)
`(since 6.1)` bool | operator>=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

## Detailed Description

QJSPrimitiveValue supports most of the primitive types defined in the ECMA-262 standard, in particular Undefined, Boolean, Number, and String. Additionally, you can store a JavaScript null in a QJSPrimitiveValue and as a special case of Number, you can store an integer value.

All those values are stored immediately, without interacting with the JavaScript heap. Therefore, you can pass QJSPrimitiveValues between different JavaScript engines. In contrast to QJSManagedValue, there is also no danger in destroying a QJSPrimitiveValue from a different thread than it was created in. On the flip side, QJSPrimitiveValue does not hold a reference to any JavaScript engine.

QJSPrimitiveValue implements the JavaScript arithmetic and comparison operators on the supported types in JavaScript semantics. Types are coerced like the JavaScript engine would coerce them if the operators were written in a JavaScript expression.

The JavaScript Symbol type is not supported as it is of very limited utility regarding arithmetic and comparison operators, the main purpose of QJSPrimitiveValue. In particular, it causes an exception whenever you try to coerce it to a number or a string, and we cannot throw exceptions without a JavaScript Engine.

## Member Type Documentation

### enum QJSPrimitiveValue::Type

This enum speicifies the types a QJSPrimitiveValue might contain.

Constant | Value | Description
--- | --- | ---
`QJSPrimitiveValue::Undefined` | `0` | The JavaScript Undefined value.
`QJSPrimitiveValue::Null` | `1` | The JavaScript null value. This is in fact not a separate JavaScript type but a special value of the Object type. As it is very common and storable without JavaScript engine, it is still supported.
`QJSPrimitiveValue::Boolean` | `2` | A JavaScript Boolean value.
`QJSPrimitiveValue::Integer` | `3` | An integer. This is a special case of the JavaScript Number type. JavaScript does not have an actual integer type, but the ECMA-262 standard contains rules on how to transform a Number in order to prepare it for certain operators that only make sense on integers, in particular the bit shift operators. QJSPrimitiveValue's Integer type represents the result of such a transformation.
`QJSPrimitiveValue::Double` | `4` | A JavaScript Number value.
`QJSPrimitiveValue::String` | `5` | A JavaScript String value.

## Member Function Documentation

### `[constexpr, since 6.6]` const void *QJSPrimitiveValue::constData() const

### `[constexpr, since 6.6]` const void *QJSPrimitiveValue::data() const

Returns a pointer to the contained value as a generic void* that cannot be written to.

This function was introduced in Qt 6.6.

### `[default]` QJSPrimitiveValue::QJSPrimitiveValue()

Creates a QJSPrimitiveValue of type Undefined.

### `[default]` QJSPrimitiveValue::QJSPrimitiveValue(QJSPrimitiveNull null)

Creates a QJSPrimitiveValue of value null and type Null.

### `[default]` QJSPrimitiveValue::QJSPrimitiveValue(QJSPrimitiveUndefined undefined)

Creates a QJSPrimitiveValue of value undefined and type Undefined.

### `[default]` QJSPrimitiveValue::QJSPrimitiveValue(bool value)

Creates a QJSPrimitiveValue of value value and type Boolean.

### `[default]` QJSPrimitiveValue::QJSPrimitiveValue(class QString value)

Creates a QJSPrimitiveValue of value value and type String.

### `[explicit noexcept]` QJSPrimitiveValue::QJSPrimitiveValue(const QVariant &value)

Creates a QJSPrimitiveValue from the contents of value if those contents can be stored in QJSPrimtiveValue. Otherwise this results in a QJSPrimitiveValue of type Undefined.

### `[default]` QJSPrimitiveValue::QJSPrimitiveValue(double value)

Creates a QJSPrimitiveValue of value value and type Double.

### `[explicit noexcept]` QJSPrimitiveValue::QJSPrimitiveValue(int value)

Creates a QJSPrimitiveValue of value value and type Integer.

### `[default, since 6.4]` QJSPrimitiveValue::QJSPrimitiveValue(int type, const void *value)

Creates a QJSPrimitiveValue of type type, and initializes with value if type can be stored in QJSPrimtiveValue. value must not be nullptr in that case. If type cannot be stored this results in a QJSPrimitiveValue of type Undefined.

Note that you have to pass the address of the variable you want stored.

Usually, you never have to use this constructor, use the one taking QVariant instead.

This function was introduced in Qt 6.4.

### `[constexpr, since 6.6]` void *QJSPrimitiveValue::data()

Returns a pointer to the contained data as a generic void* that can be written to.

This function was introduced in Qt 6.6.

### `[constexpr]` bool QJSPrimitiveValue::equals(const QJSPrimitiveValue &other) const

Performs the JavaScript '==' operation on this QJSPrimitiveValue and other, and returns the result.

### `[constexpr, since 6.6]` int QJSPrimitiveValue::metaType() const

Returns the QMetaType of the value stored in the QJSPrimitiveValue.

This function was introduced in Qt 6.6.

### `[constexpr]` bool QJSPrimitiveValue::strictlyEquals(const QJSPrimitiveValue &other) const

Performs the JavaScript '===' operation on this QJSPrimitiveValue and other, and returns the result.

### `[since 6.6]` template <QJSPrimitiveValue::Type type> QJSPrimitiveValue QJSPrimitiveValue::to() const

Coerces the value to the specified type and returns the result as a new QJSPrimitiveValue.

This function was introduced in Qt 6.6.

See also toBoolean(), toInteger(), toDouble(), and toString().

### `[constexpr]` bool QJSPrimitiveValue::toBoolean() const

Returns the value coerced a boolean by JavaScript rules.

### `[constexpr]` double QJSPrimitiveValue::toDouble() const

Returns the value coerced to a JavaScript Number by JavaScript rules.

### `[constexpr]` int QJSPrimitiveValue::toInteger() const

Returns the value coerced to an integral 32bit number by the rules JavaScript would apply when preparing it for a bit shift operation.

### int QJSPrimitiveValue::toString() const

Returns the value coerced to a JavaScript String by JavaScript rules.

### `[constexpr]` QJSPrimitiveValue::Type QJSPrimitiveValue::type() const

Returns the type of the QJSPrimitiveValue.

## Related Non-Members

### `[constexpr, since 6.1]` bool operator!=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '!==' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[since 6.1]` QJSPrimitiveValue operator*(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '*' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[since 6.1]` QJSPrimitiveValue operator+(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Perfoms the JavaScript '+' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[since 6.1]` QJSPrimitiveValue operator-(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '-' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[since 6.1]` QJSPrimitiveValue operator/(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '/' operation between lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[constexpr, since 6.1]` bool operator<(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '<' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[constexpr, since 6.1]` bool operator<=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '<=' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[constexpr, since 6.1]` bool operator==(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '===' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[constexpr, since 6.1]` bool operator>(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '>' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.

### `[constexpr, since 6.1]` bool operator>=(const QJSPrimitiveValue &lhs, const QJSPrimitiveValue &rhs)

Performs the JavaScript '>=' operation on lhs and rhs, and returns the result.

This function was introduced in Qt 6.1.