1# Comparing JS Values Using JSVM-API 2 3## Introduction 4 5This 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 (===). 6 7## Basic Concepts 8 9Strictly 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. 10 11## Available APIs 12 13| API | Description | 14|----------------------------|-------------------------------------| 15| OH_JSVM_StrictEquals | Checks whether two **JSVM_Value** objects are strictly equal.| 16 17## Example 18 19If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ code involved in comparing JS values. 20 21### OH_JSVM_StrictEquals 22 23Use **OH_JSVM_StrictEquals** to check whether two JS values are strictly equal. 24 25CPP code: 26 27```cpp 28// hello.cpp 29#include "napi/native_api.h" 30#include "ark_runtime/jsvm.h" 31#include <hilog/log.h> 32// Define OH_JSVM_StrictEquals. 33static JSVM_Value IsStrictEquals(JSVM_Env env, JSVM_CallbackInfo info) 34{ 35 // Obtain the input parameters. 36 size_t argc = 2; 37 JSVM_Value args[2] = {nullptr}; 38 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 39 // Call OH_JSVM_StrictEquals to check whether two given JS values are strictly equal. 40 bool result = false; 41 JSVM_Status status = OH_JSVM_StrictEquals(env, args[0], args[1], &result); 42 if (status != JSVM_OK) { 43 OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_StrictEquals: failed"); 44 } else { 45 OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_StrictEquals: success: %{public}d", result); 46 } 47 JSVM_Value isStrictEqual; 48 OH_JSVM_GetBoolean(env, result, &isStrictEqual); 49 return isStrictEqual; 50} 51// Register the IsStrictEquals callback. 52static JSVM_CallbackStruct param[] = { 53 {.data = nullptr, .callback = IsStrictEquals}, 54}; 55static JSVM_CallbackStruct *method = param; 56// Set a property descriptor named isStrictEquals and associate it with a callback. This allows the isStrictEquals callback to be called from JS. 57static JSVM_PropertyDescriptor descriptor[] = { 58 {"isStrictEquals", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 59}; 60// Call the C++ code from JS. 61const char* srcCallNative = R"JS(data = '123';value = '123';isStrictEquals(data,value);)JS"; 62``` 63 64**Expected output** 65 66```ts 67JSVM OH_JSVM_StrictEquals: success: 1 68``` 69