---
schema_version: "1"
id: 81cf0caf66d010b5d4c16f2db00788838e9ffe18ee460bcba94415a7a4466515
title: Working with a Component Body in JavaScript
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/aura-platform/guides/aura/js/js-cmp-body.md
canonical_url: https://developer.salesforce.com/docs/platform/aura-platform/guide/js-cmp-body.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 47326b351dd0cbcd26fea17efd19b43dd8e1af1a1aee5d3f88318891be4e35ea
products:
  - Lightning Aura Components Developer Guide
  - Platform Developer Content
---

# Working with a Component Body in JavaScript

These are useful and common patterns for working with a component’s body in JavaScript.

In these examples, `cmp` is a reference to a component in your JavaScript code. It’s usually easy to get a reference to a component in JavaScript code. Remember that the `body` attribute is an array of components, so you can use the JavaScript `Array` methods on it.

**note:** When you use `cmp.set("v.body", ...)` to set the component body, you must explicitly include `{!v.body}` in your component markup.

## Replace a Component's Body

To replace the current value of a component’s body with another component:

```javascript
// newCmp is a reference to another component
cmp.set("v.body", newCmp);
```

## Clear a Component's Body

To clear or empty the current value of a component’s body:

```javascript
cmp.set("v.body", []);
```

## Append a Component to a Component's Body

To append a `newCmp` component to a component’s body:

```javascript
var body = cmp.get("v.body");
// newCmp is a reference to another component
body.push(newCmp);
cmp.set("v.body", body);
```

## Prepend a Component to a Component's Body

To prepend a `newCmp` component to a component’s body:

```javascript
var body = cmp.get("v.body");
body.unshift(newCmp);
cmp.set("v.body", body);
```

## Remove a Component from a Component's Body

To remove an indexed entry from a component’s body:

```javascript
var body = cmp.get("v.body");
// Index (3) is zero-based so remove the fourth component in the body
body.splice(3, 1);
cmp.set("v.body", body);
```

**See Also**  


-   [Component Body](https://developer.salesforce.com/docs/platform/aura-platform/components/components-body.md)
-   [Working with Attribute Values in JavaScript](https://developer.salesforce.com/docs/platform/aura-platform/guide/js-attr-values.md)

