<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "JavaScriptCore",
  "identifier" : "/documentation/JavaScriptCore/JSManagedValue",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "JavaScriptCore"
    ],
    "preciseIdentifier" : "c:objc(cs)JSManagedValue"
  },
  "title" : "JSManagedValue"
}
-->

# JSManagedValue

A JavaScript value with conditional retain behavior to provide automatic memory management.

```
class JSManagedValue
```

## Overview

The primary use case for a managed value is to store a JavaScript value in an Objective-C or Swift object that exports to JavaScript.

> Important:
> Don’t store a nonmanaged ``doc://com.apple.javascriptcore/documentation/JavaScriptCore/JSValue`` object in a native object that exports to JavaScript. Because a ``doc://com.apple.javascriptcore/documentation/JavaScriptCore/JSValue`` object references its enclosing ``doc://com.apple.javascriptcore/documentation/JavaScriptCore/JSContext`` object, this action creates a retain cycle, preventing deallocation of the context.

A managed value’s *conditional retain* behavior ensures retention of its underlying JavaScript value as long as either of the following conditions is true:

- The JavaScript value is reachable through the JavaScript object graph (that is, not subject to JavaScript garbage collection).
- The [`JSManagedValue`](/documentation/JavaScriptCore/JSManagedValue) object is reachable through the Objective-C or Swift object graph, as you report to the JavaScriptCore virtual machine using the [`addManagedReference(_:withOwner:)`](/documentation/JavaScriptCore/JSVirtualMachine/addManagedReference(_:withOwner:)) method.

However, if neither of these conditions is true, the managed value sets its [`value`](/documentation/JavaScriptCore/JSManagedValue/value) property to `nil`, releasing the underlying [`JSValue`](/documentation/JavaScriptCore/JSValue) object.

> Note:
> On its own, a ``doc://com.apple.javascriptcore/documentation/JavaScriptCore/JSManagedValue`` object behaves similarly to an ARC weak reference to its underlying ``doc://com.apple.javascriptcore/documentation/JavaScriptCore/JSValue`` object—that is, if you don’t use the ``doc://com.apple.javascriptcore/documentation/JavaScriptCore/JSVirtualMachine/addManagedReference(_:withOwner:)`` method to add conditional retain behavior, the managed value’s ``doc://com.apple.javascriptcore/documentation/JavaScriptCore/JSManagedValue/value`` property automatically becomes `nil` when the JavaScript garbage collector destroys the underlying JavaScript value.

## Topics

### Creating a Managed Value

[`init(value:)`](/documentation/JavaScriptCore/JSManagedValue/init(value:))

Initializes a managed value with the specified JavaScript value.

[`managedValueWithValue:`](/documentation/JavaScriptCore/JSManagedValue/managedValueWithValue:)

Creates a managed value with the specified JavaScript value.

[`init(value:andOwner:)`](/documentation/JavaScriptCore/JSManagedValue/init(value:andOwner:))

Creates a managed value and associates it with an owner.

### Accessing the Managed Value

[`value`](/documentation/JavaScriptCore/JSManagedValue/value)

The managed value’s underlying JavaScript value.



---

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)