JavaScript String bold() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated

What You’ll Learn

String.prototype.bold() is a deprecated HTML wrapper method (same API as MDN String.prototype.bold()). It returns a string that wraps your text in a <b> tag. Learn what it builds, how <b> differs from <strong>, and how to replace the wrapper with DOM APIs or CSS — with five examples and try-it labs.

01

Kind

Instance method

02

Returns

HTML string

03

Status

Deprecated

04

Params

None

05

Mutates?

No

06

Prefer

DOM / font-weight

Introduction

Early JavaScript included helpers that wrapped text in HTML presentation tags — bold(), big(), blink(), italics(), and others. They made it easy to inject markup with innerHTML.

Today those wrappers are deprecated. bold() may still return a string for compatibility. The <b> element remains valid HTML, but you should create it with the DOM (or use CSS / <strong>) — not string methods.

💡
Learn it, don’t ship it

Study bold() so you can recognize legacy code. For new pages, create real elements with document.createElement() or apply font-weight instead of building <b> strings.

This page is part of JavaScript String Methods. Related topics include blink() and big().

Understanding the bold() Method

Calling str.bold() does not create a live DOM node and does not take arguments. It concatenates an HTML string: <b> + text + </b>.

  • It is an instance method on strings (auto-boxed if needed).
  • It returns a new string — the original is unchanged.
  • No parameters — just str.bold().
  • Prefer DOM APIs or CSS font-weight for real UI.

📝 Syntax

General form of String.prototype.bold:

JavaScript
str.bold()

Parameters

None.

Return value

A string beginning with a <b> start tag, then the text of str, then a </b> end tag.

Common patterns

JavaScript
"Hello, world".bold();
// 'Hello, world'

// Prefer this in new code:
const el = document.createElement("b");
el.textContent = "Hello, world";
// document.body.appendChild(el);

⚡ Quick Reference

GoalCode
Legacy HTML stringstr.bold()
Result shape<b>...</b>
Modern DOMdocument.createElement("b")
Use in new apps?No — deprecated

🔍 At a Glance

Four facts to remember about String.bold().

Returns
string

HTML markup text

Status
deprecated

Compatibility only

Tag
<b>

Still valid HTML

Replace with
createElement

DOM, not strings

📋 bold() vs createElement("b")

str.bold()createElement("b")
ResultHTML stringLive DOM element
Valid HTML today?Yes (tag OK; method deprecated)Yes
Recommended?NoYes
Best forReading legacy codeNew UI / apps

Examples Gallery

Examples follow MDN String.bold() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See the exact HTML string bold() builds.

Example 1 — Basic bold()

MDN-style call that wraps text in <b>.

JavaScript
const contentString = "Hello, world";
contentString.bold();
// 'Hello, world'
Try It Yourself

How It Works

The method returns markup text only. Nothing is added to the document until you assign it somewhere (for example innerHTML).

Example 2 — Empty String and Special Characters

Tags are still produced for empty text; content is not HTML-escaped.

JavaScript
"".bold();           // ''
"A < B".bold();      // 'A < B'
typeof "x".bold();   // "string"
Try It Yourself

How It Works

Unlike some attribute wrappers, bold() does not escape < inside the text. That is another reason to avoid injecting untrusted strings via innerHTML.

📈 Practical Patterns

Legacy injection patterns and the modern DOM replacement.

Example 3 — Injecting with innerHTML (Legacy)

How older demos used the returned string — prefer the DOM in new apps.

JavaScript
const contentString = "Hello, world";
const html = contentString.bold();
// document.body.innerHTML = html;  // legacy pattern — avoid in new apps

console.log(html);
// Prefer createElement("b") in new apps
Try It Yourself

How It Works

The returned string is only markup text. Prefer creating a real <b> or <strong> element with the DOM instead of assigning HTML strings.

Example 4 — Other HTML Wrappers Nearby

Recognize the same pattern family — all deprecated.

JavaScript
"Hi".bold();    // 'Hi'
"Hi".big();     // 'Hi'
"Hi".blink();   // 'Hi'
"Hi".italics(); // 'Hi'
Try It Yourself

How It Works

These helpers share one idea: return presentational HTML strings. Modern code styles elements with CSS (and semantic tags when needed).

Example 5 — Modern Replacement with the DOM

MDN’s recommended approach — create a real element instead of an HTML string.

JavaScript
const contentString = "Hello, world";
const elem = document.createElement("b");
elem.textContent = contentString;

// document.body.appendChild(elem);
console.log(elem.tagName);
console.log(elem.outerHTML);
Try It Yourself

How It Works

You get a live element you can style, attach listeners to, and keep separate from untrusted HTML concatenation. Use <strong> when the meaning is important, or CSS font-weight for look only.

🚀 Common Use Cases

  • Reading legacy tutorials — recognize HTML wrapper methods in old samples.
  • Migrating old scripts — replace bold() with createElement() or CSS font-weight.
  • Teaching string immutability — show that methods return new strings.
  • Not for new apps — do not use bold() to build HTML in new apps.
  • Semantic emphasis — prefer <strong> for importance and CSS for look-only weight.
  • Safer markup — prefer textContent + CSS over innerHTML strings.

🧠 How bold() Builds Markup

1

Start with text

You call str.bold() on a string receiver.

Input
2

Wrap with tags

Concatenate <b> + text + </b>.

Wrap
3

Return an HTML string

No DOM node is created — only text markup.

Result
4

Prefer the DOM

Create a real <b> or <strong> element with the DOM.

📝 Notes

  • bold() is deprecated — standardized only for compatibility.
  • The method is deprecated; the <b> element itself remains valid HTML.
  • <b> draws attention; <strong> marks strong importance — pick the right one.
  • It returns a string, not a DOM node.
  • It takes no parameters.
  • Other HTML wrappers (big(), blink(), italics(), …) share the same fate.
  • Avoid assigning untrusted strings to innerHTML.

Browser & Runtime Support

String.prototype.bold() is still widely implemented for compatibility, but it is deprecated. Do not use it in new projects.

Deprecated · Legacy

String.bold()

Available in modern browsers for old code, but MDN recommends DOM APIs such as createElement() instead.

Legacy Compatibility only
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
bold() Avoid in new code

Bottom line: Recognize bold() in legacy samples. For new UI, create elements with the DOM or use CSS font-weight — never rely on HTML wrapper string methods.

Conclusion

String.prototype.bold() builds a <b>...</b> HTML string. The method is deprecated — useful for history and migrating old code — not for writing new features.

Continue with blink(), at(), or the String methods hub.

💡 Best Practices

✅ Do

  • Use document.createElement("b") or CSS font-weight
  • Use <strong> when the text is important
  • Prefer textContent over string-built HTML
  • Replace bold() when you touch legacy files
  • Learn wrappers only to recognize deprecated APIs

❌ Don’t

  • Use bold() in new production code
  • Assume string-built HTML is the best way to bold text
  • Inject untrusted bold() output via innerHTML
  • Expect bold() to return a live element
  • Mix HTML wrappers with modern component frameworks

Key Takeaways

Knowledge Unlocked

Five things to remember about String.bold()

Deprecated HTML wrapper — prefer createElement or CSS.

5
Core concepts
⚠️ 02

Status

deprecated

Legacy
03

Markup

<b> OK

HTML
04

Replace

createElement

Modern
05

Mutates

no

Immutable

❓ Frequently Asked Questions

String.prototype.bold() returns an HTML string that wraps the text in a <b> element — for example "Hello".bold() returns '<b>Hello</b>'. It takes no parameters.
Yes. MDN marks all HTML wrapper methods as deprecated. Prefer document.createElement(), CSS font-weight, or a semantic <strong> element when the text is important.
Yes. Unlike <blink> or <big>, <b> is still in HTML. Use it to draw attention without extra importance. Use <strong> when the text has strong importance, or CSS font-weight for visual weight alone.
No. Strings are immutable. bold() returns a new string containing HTML markup. The original text stays the same.
Use document.createElement("b") or createElement("strong"), set textContent, and append the element. Or apply CSS font-weight without building HTML strings.
It remains widely available for compatibility, but you should not use it in new code. Prefer DOM APIs or CSS.
Did you know?

HTML keeps both <b> and <strong>. Use <b> for stylistic attention without changing importance, and <strong> when the meaning is strongly important. CSS font-weight covers look-only bolding.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

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.

8 people found this page helpful