# JsExpression

> JsExpression marks a string as a JavaScript expression.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › JsExpression

| class | `yii\web\JsExpression` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/JsExpression.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/JsExpression.php#L22) |

`JsExpression` marks a string as a JavaScript expression.

When using [`Json::encode()`](/docs/api/yii/helpers/basejson/index.md#method-encode) or [`Json::htmlEncode()`](/docs/api/yii/helpers/basejson/index.md#method-htmlEncode) to encode a value, `JsExpression` objects
will be specially handled and encoded as a JavaScript expression instead of a string.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-expression"></span>`$expression` | `string` | The JavaScript expression represented by this object Default: `null`. | `JsExpression` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Constructor. | `JsExpression` |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`__toString()`](#method-__toString) | The PHP magic function converting an object into a string. | `JsExpression` |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(string $expression, array $config = [])
```

Constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$expression` | `string` | The JavaScript expression represented by this object |
| `$config` | `array` | Additional configurations for this object |

### __toString() <span id="method-__toString"></span>

```php
public function __toString(): string
```

The PHP magic function converting an object into a string.

**Returns**

`string`: The JavaScript expression.