This topic walks you through on how to use JSVM-API to check whether two JavaScript (JS) values are strictly equal (equal in both value and type). The API provided is equivalent to the JS strict equality operator (===). This API avoids type conversion and loose equality comparison, ensuring equal values and types.
Strictly equal: If two values are strictly equal, they are equal in both value and type. When type conversion is considered, if the values being compared are of different types, false will be returned even if the values are the same.
| API | Description |
|---|---|
| OH_JSVM_StrictEquals | Checks whether two JSVM_Value objects are strictly equal |
If you are just starting out with JSVM-API, see JSVM-API Development Process. The following demonstrates only the C++ code involved in comparing JS values.
Use OH_JSVM_StrictEquals to check whether two JS values are strictly equal.
CPP code:
#include "napi/native_api.h"
#include "ark_runtime/jsvm.h"
#include "hilog/log.h"
// ...
// Sample method for OH_JSVM_StrictEquals
static JSVM_Value IsStrictEquals(JSVM_Env env, JSVM_CallbackInfo info)
{
// Receive two input parameters.
size_t argc = 2;
JSVM_Value args[2] = {nullptr};
OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
// Call the OH_JSVM_StrictEquals API to determine whether two given JavaScript values are strictly equal.
bool result = false;
JSVM_Status status = OH_JSVM_StrictEquals(env, args[0], args[1], &result);
if (status != JSVM_OK) {
OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_StrictEquals: failed");
} else {
OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_StrictEquals: success: %{public}d", result);
}
JSVM_Value isStrictEqual = nullptr;
OH_JSVM_GetBoolean(env, result, &isStrictEqual);
return isStrictEqual;
}
// Register the IsStrictEquals callback.
static JSVM_CallbackStruct param[] = {
{.data = nullptr, .callback = IsStrictEquals},
};
static JSVM_CallbackStruct *method = param;
// Alias for the IsStrictEquals method, for JS invocation.
static JSVM_PropertyDescriptor descriptor[] = {
{"isStrictEquals", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
};
// Sample test JS
const char* SRC_CALL_NATIVE = R"JS(data = '123';value = '123';isStrictEquals(data,value);)JS"; Expected result:
JSVM OH_JSVM_StrictEquals: success: 1