<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.5.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "JavaScriptCore",
  "identifier" : "/documentation/JavaScriptCore/JSValueProtect(_:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "JavaScriptCore"
    ],
    "preciseIdentifier" : "c:@F@JSValueProtect"
  },
  "title" : "JSValueProtect(_:_:)"
}
-->

# JSValueProtect(_:_:)

Protects a JavaScript value from garbage collection.

```
func JSValueProtect(_ ctx: JSContextRef!, _ value: JSValueRef!)
```

## Parameters

`ctx`

The execution context to use.

`value`

The [`JSValueRef`](/documentation/JavaScriptCore/JSValueRef) to protect.

## Discussion

Use this method when you want to store a [`JSValueRef`](/documentation/JavaScriptCore/JSValueRef) in a global or on the heap, where the garbage collector can’t discover your reference to it.

You can protect a value multiple times and must unprotect it an equal number of times before it becomes eligible for garbage collection.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)