Lodash _.stubString() Method

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Util utilities

What You’ll Learn

By the end of this tutorial, you’ll use Lodash’s _.stubString() as a named “always empty string” function—for defaults, fallbacks, and functional callbacks.

01

Core Syntax

_.stubString()

02

Returns ''

Empty string.

03

Args ignored

Always ''.

04

Defaults

Param fallbacks.

05

Fn reference

_.stubString

06

vs constant

constant('')

What Is _.stubString()?

_.stubString is a zero-argument function that always returns the empty string ''. Call it as _.stubString() when you need '' right now, or pass _.stubString (without parentheses) when another API wants a function that produces empty strings—similar to writing () => ''.

💡
Beginner tip — function vs string value

_.stubString() gives you the value ''. _.stubString (no parentheses) is the function itself—pass it when Lodash expects a callback like _.times or _.map.

It sits in Lodash’s stub helper family alongside _.stubObject() and _.stubTrue()—small building blocks for readable functional code.

📝 Syntax

Invoke with no arguments:

javascript
_.stubString()

Syntax Rules

  • _.stubString — the function reference (pass to callbacks).
  • _.stubString() — call it to get '' immediately.
  • Ignores arguments_.stubString('hello', 42) still returns ''.
  • Length zero_.stubString().length === 0.
  • Primitive string — returns '', not null or undefined.
javascript
import stubString from "lodash/stubString";



stubString();

// ''

⚡ Quick Reference

TaskCode patternNotes
Get empty string_.stubString()Immediate ''
Default parameterfn(label = _.stubString())When omitted
Fallback returnreturn data || _.stubString()Always string
Callback ref_.times(3, _.stubString)Fn reference
Arrow equivalent() => ''Same idea
Equivalent_.constant('')Same '' each call
Returns
''

Empty string

Type
Function

Zero-arg

Return type
string

Primitive

Category
Util

Stub

🧰 Parameters

_.stubString takes no parameters—only returns a value when invoked:

arguments None

No configuration. Call _.stubString() with an empty argument list.

_.stubString()
return string

Empty string ''—falsy in boolean context but still a valid string type.

''
arguments Ignored

Any values passed when stubString is used as a callback are discarded.

_.stubString('x', 1)
as callback Pattern

Pass _.stubString without () when an iteratee should produce ''.

_.times(n, _.stubString)

Need always-true or always-false instead? See _.stubTrue() and _.stubFalse() in the stub helper series.

Examples Gallery

Practical _.stubString() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.

📚 Getting Started

Call stubString and inspect the result.

Example 1 — Basic empty string

The simplest use—get '' in one expression.

javascript
const empty = _.stubString();



console.log(empty);

console.log(typeof empty);

console.log(empty.length);

console.log(empty === '');

// (empty line)

// "string"

// 0

// true
Try It Yourself

How It Works

stubString is a named alias for the common pattern “give me an empty string now.”

Example 2 — Arguments are ignored

Callers may pass values—stubString always returns ''.

javascript
console.log(_.stubString());

console.log(_.stubString('hello'));

console.log(_.stubString(1, true, {}));

// (empty)

// (empty)

// (empty)
Try It Yourself

How It Works

Like other stub helpers, stubString ignores every argument—useful when Lodash passes index or collection values you do not need.

📈 Practical Patterns

Defaults, merging, and safe fallbacks.

Example 3 — Default function parameter

When callers omit a string argument, stub supplies '' for that invocation.

javascript
function greet(name = _.stubString()) {

  return "Hello, " + name + "!";

}



console.log(greet());

console.log(greet("Ada"));

// Hello, !

// Hello, Ada!

How It Works

Plain name = '' also works in modern JS; stubString signals intent in Lodash-heavy codebases.

Example 4 — Fallback return value

Guarantee a string type even when API data is missing.

javascript
function getLabel(apiResult) {

  const label = apiResult && apiResult.label;

  return label || _.stubString();

}



console.log(getLabel({ label: "Save" }));

console.log(getLabel(null));

// Save

// (empty string)
Try It Yourself

How It Works

Callers can always call string methods like .trim() on the result—empty means “no label yet.”

Example 5 — Pass as callback to _.times

Use the function reference when iteratees should produce empty strings.

javascript
const slots = _.times(3, _.stubString);



console.log(slots);

console.log(slots.every(function (s) { return s === ''; }));

// ["", "", ""]

// true

How It Works

Pass _.stubString without ()—Lodash invokes it each iteration. Do not assign _.stubString() when you need a callable function.

🚀 Beyond the Basics

stubString vs alternatives in the stub family.

Example 6 — stubString vs '' vs constant vs arrow

Pick the right empty-string tool for the job.

javascript
// Immediate empty string

console.log(_.stubString());



// ES default — also common

function fn(x = '') { return x; }



// Equivalent fixed return

const emptyFn = _.constant('');

console.log(emptyFn() === _.stubString());



// Arrow equivalent of stubString

console.log(typeof (() => '')() === 'string');

// true for all '' producers

When to use which

Use stubString for named functional style; '' or = '' when plain JavaScript is clearer; constant('') behaves the same for immutable strings.

🧠 How _.stubString() Works

1

Invoke stub

Call _.stubString() or pass _.stubString to an iteratee.

Call
2

Return ''

Lodash evaluates the function and yields the empty string primitive.

Create
3

Use as normal string

Concat, template literals, .trim(), length checks—standard string operations apply.

Consume
=

Empty string

length === 0, ready for safe defaults and fallbacks.

📝 Notes

  • Returns the string primitive ''—not null or undefined.
  • Strings are immutable—unlike objects, there is no “fresh reference” concern.
  • Pass _.stubString without parentheses when an API expects a zero-arg producer function.
  • return text || _.stubString() treats empty string as falsy—use nullish coalescing (??) if '' is valid data.
  • Part of the stub helper family—next: _.stubTrue().
  • Previous in the series: _.stubObject().

Conclusion

_.stubString() is a tiny but expressive helper: a function that always produces the empty string. Use it for defaults, fallback returns, and callback slots when you want Lodash-flavored clarity over raw ''.

For strings, _.constant('') behaves the same in practice—choose stubString when readability in the stub family matters.

💡 Best Practices

✅ Do

  • Use _.stubString() for readable empty-string defaults in Lodash code
  • Return _.stubString() when APIs must always yield a string type
  • Pass _.stubString as a callback when iteratees should produce ''
  • Prefer ?? over || when empty string is valid data
  • Use stubString instead of misassigning _.stubString() when you need a function reference

❌ Don’t

  • Assign const fn = _.stubString() expecting a callable—use _.stubString instead
  • Use || stubString() when '' should be preserved—use ??
  • Confuse stubString with noop—it returns '' specifically, not undefined
  • Reach for stubString when plain = '' is clearer for your team
  • Expect separate string references—primitives do not work like mutable objects

Key Takeaways

Knowledge Unlocked

Five things to remember about _.stubString()

Use these points for empty-string defaults and fallbacks.

5
Core concepts
🔄 02

Args ignored

Always ''.

Behavior
03

Defaults

Params.

Usage
📝 04

Callback

Pass ref.

Pattern
🛠 05

vs constant

Shared vs new.

Compare

❓ Frequently Asked Questions

_.stubString is a zero-argument function. Calling _.stubString() returns the empty string ''. It is a named helper for “always give me an empty string” in functional Lodash code.
'' is a value. _.stubString is a function you call (or pass by reference) when an API expects a function that produces empty strings—default parameters, _.times, _.map, or fallback returns.
Strings are primitives—each call returns '', the same empty string value. Unlike objects or arrays, there is no separate reference to worry about; mutation is not a concern.
_.stubString is equivalent to _.constant('') for practical use. Both always return '' and ignore arguments. stubString reads clearer in Lodash stub-style code.
Yes: function fn(label = _.stubString()) works. When the argument is omitted, the stub runs and supplies '' for that invocation—same as name = '' in plain JavaScript.
Use it for empty-string fallbacks (return data || _.stubString()), optional string parameters, map/times callbacks that should yield '', and anywhere () => '' reads well but you prefer a Lodash name.
Did you know?

Lodash documents _.times(2, _.stubString) in the official docs—passing _.stubString by reference lets _.times invoke it each iteration and collect ['', ''].

Practice _.stubString() in the Live Editor

Try basic empty strings, argument-ignore checks, and times-callback patterns.

Open Try It editor →

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.

6 people found this page helpful