# perldoc > BSON::Code

---
type: CommandReference
command: BSON::Code
mode: perldoc
section:
source: perldoc
---

## Quick Reference
- `bson_code('function() { return 1; }')` — create a BSON code object
- `bson_code('function() { return x; }', { x => 42 })` — with a scope
- `$code->length` — get length of the JavaScript string
- `$code->TO_JSON` — export as extended JSON (if `BSON_EXTJSON` enabled)

## Name
BSON type wrapper for Javascript code

## Synopsis
perl
use BSON::Types ':all';
$code = bson_code( $javascript );
$code = bson_code( $javascript, $scope );
## Attributes & Methods
- `code` — string of JavaScript code, defaults to `""`
- `scope` — optional hashref of scope variables, defaults to `undef`
- `length` — returns the length of the `code` attribute
- `TO_JSON` — if `BSON_EXTJSON` is set, returns a hashref compatible with MongoDB extended JSON; otherwise throws an error

## See Also
- [BSON::Types](https://www.chedong.com/phpMan.php/perldoc/BSON%3A%3ATypes/markdown)