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++ and ArkTS code related to the comparison. 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// Register the IsStrictEquals callback. 33static JSVM_CallbackStruct param[] = { 34 {.data = nullptr, .callback = IsStrictEquals}, 35}; 36static JSVM_CallbackStruct *method = param; 37// Set a property descriptor named isStrictEquals and associate it with a callback. This allows the isStrictEquals callback to be called from JS. 38static JSVM_PropertyDescriptor descriptor[] = { 39 {"isStrictEquals", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 40}; 41// Define OH_JSVM_StrictEquals. 42static JSVM_Value IsStrictEquals(JSVM_Env env, JSVM_CallbackInfo info) 43{ 44 // Obtain the input parameters. 45 size_t argc = 2; 46 JSVM_Value args[2] = {nullptr}; 47 OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr); 48 // Call OH_JSVM_StrictEquals to check whether two given JS values are strictly equal. 49 bool result = false; 50 JSVM_Status status = OH_JSVM_StrictEquals(env, args[0], args[1], &result); 51 if (status != JSVM_OK) { 52 OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_StrictEquals: failed"); 53 } else { 54 OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_StrictEquals: success: %{public}d", result); 55 } 56 JSVM_Value isStrictEqual; 57 OH_JSVM_GetBoolean(env, result, &isStrictEqual); 58 return isStrictEqual; 59} 60``` 61 62ArkTS code: 63 64```ts 65let script: string = ` 66 let data = '123'; 67 let value = 123; 68 isStrictEquals(data,value); 69`; 70try { 71 let result = napitest.runJsVm(script); 72 hilog.info(0x0000, 'JSVM', 'isStrictEquals: %{public}s', result); 73} catch (error) { 74 hilog.error(0x0000, 'JSVM', 'isStrictEquals: %{public}s', error.message); 75} 76``` 77