Underscore.js _.clone() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Object

Definition

Create a shallow-copied clone of the provided plain object. Any nested objects or arrays will be copied by reference, not duplicated (official Underscore docs). Stable Object helper — not deprecated.

Pass the required arguments shown below. Underscore object helpers usually return a new value — check whether the source object is mutated.

Syntax

Call _.clone. Start with this line.

Underscore
_.clone(object)
CallWhat it does
_.clone(object)Primary signature.
_.clone(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.clone(object)

Parameters

ParameterDescription
objectRequired. Object or array to copy

Return Value

Return ValueDescription
*New shallow copy (or primitive as-is)

Underscore.js Examples

Example 1 — Official Docs Clone

Copy a plain object into a new container.

Underscore
var original = { name: "moe" };
var copy = _.clone(original);

copy;
// → { name: "moe" }

copy !== original;
// → true
Try It Yourself

How It Works

  1. Source. original holds name: "moe".
  2. Clone. Underscore builds a new object with the same top-level fields.
  3. Identity. copy !== original — different references.
  4. Docs sample. This is the official Underscore example.

Example 2 — Edit Top-Level Safely

Changing a top-level field on the clone leaves the original alone.

Underscore
var user = { name: "Ada", role: "admin" };
var draft = _.clone(user);

draft.role = "editor";

user.role;   // → "admin"
draft.role;   // → "editor"
Try It Yourself

How It Works

  1. Clone. draft is a new object with copied top-level keys.
  2. Edit. Assigning draft.role only changes the clone.
  3. Original safe. user.role stays "admin".
  4. Use case. Draft edits, form state, undo-friendly copies.

Example 3 — Nested Values Are Shared

Shallow copy: nested objects still point to the same place.

Underscore
var original = { name: "moe", scores: [1, 2] };
var copy = _.clone(original);

copy.scores.push(3);

original.scores;
// → [1, 2, 3]
copy.scores === original.scores;
// → true
Try It Yourself

How It Works

  1. Top-level. name and scores keys are copied onto a new object.
  2. Nested ref. Both objects hold the same array reference.
  3. Mutate. push changes that shared array.
  4. Deep need? Use structuredClone when nests must be independent.

Example 4 — Clone an Array

Arrays get a new outer array; nested items can still be shared.

Underscore
var nums = [1, 2, 3];
var copy = _.clone(nums);

copy.push(4);

nums;                 // → [1, 2, 3]
copy;                 // → [1, 2, 3, 4]
copy !== nums;        // → true

_.clone(42);          // → 42  (primitive as-is)
Try It Yourself

How It Works

  1. New array. copy is a separate array container.
  2. Push. Adding 4 only affects the clone.
  3. Source safe. nums stays [1, 2, 3].
  4. Primitives. _.clone(42) returns 42 unchanged.

Browser & Runtime Support

_.clone ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.clone()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.clone Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.clone(object) creates a shallow copy of an object or array. Official example: _.clone({name: "moe"}) → {name: "moe"} as a new object.
No. Nested objects and arrays are copied by reference. Mutating a nested value through the clone also changes the original.
Underscore has no deep-clone helper. Use structuredClone(obj) in modern environments, or Lodash _.cloneDeep for richer cases.
_.clone(obj) is like _.extend({}, obj) — always a new container. _.extend copies into a target you provide and mutates that target.
Arrays are cloned into a new array (similar spirit to slice). Top-level elements are copied; nested objects inside stay shared.
Numbers, strings, and booleans are returned as-is — they are already value types.

Next: _.tap()

Invoke an interceptor on a value and return the value — handy inside chains.

_.tap() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful