JavaScript String fixed() Method

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

What You’ll Learn

String.prototype.fixed() is a deprecated HTML wrapper method (same API as MDN String.prototype.fixed()). It returns a string that wraps your text in a <tt> (teletype / fixed-width) tag. Learn what it builds, why that element is obsolete, and how to replace it with CSS font-family: monospace — 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

CSS monospace

Introduction

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

Today those wrappers are deprecated. fixed() still works in browsers for compatibility, but the <tt> element itself was removed from HTML. Fixed-width (monospace) fonts belong in CSS — or in semantic tags like <code> when you mean computer code.

💡
Learn it, don’t ship it

Study fixed() so you can recognize legacy code. For new pages, style text with font-family: monospace (or use <code>) instead of building <tt> strings.

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

Understanding the fixed() Method

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

  • It is an instance method on strings (auto-boxed if needed).
  • It returns a new string — the original is unchanged.
  • No parameters — just str.fixed().
  • Prefer CSS font-family: monospace (or <code>) for real UI.

📝 Syntax

General form of String.prototype.fixed:

JavaScript
str.fixed()

Parameters

None.

Return value

Always an HTML string wrapped in <tt>. Special cases:

SituationReturns
Any non-empty string<tt> + text + </tt> (e.g. "Hi".fixed()"<tt>Hi</tt>")
Empty string"<tt></tt>"
Text with < / &Not escaped — raw characters stay inside the tags
Valid modern HTML?No<tt> was removed from the HTML specification

Common patterns

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

// Prefer this in new code:
const el = document.getElementById("yourElemId");
el.style.fontFamily = "monospace";

// Or semantic markup for code:
const code = document.createElement("code");
code.textContent = "Hello, world";

⚡ Quick Reference

GoalCode
Legacy HTML stringstr.fixed()
Result shape<tt>...</tt>
Modern monospaceel.style.fontFamily = "monospace"
Inline codedocument.createElement("code")
Use in new apps?No — deprecated

🔍 At a Glance

Four facts to remember about String.fixed().

Returns
string

HTML markup text

Status
deprecated

Compatibility only

Tag
<tt>

Removed from HTML

Replace with
monospace

CSS / <code>

📋 fixed() vs CSS font-family

str.fixed()el.style.fontFamily
ResultHTML stringStyled live element
Valid HTML today?No (<tt> removed)Yes
Recommended?NoYes
Best forReading legacy codeNew UI / apps

Examples Gallery

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

📚 Getting Started

See the exact HTML string fixed() builds.

Example 1 — Basic fixed()

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

JavaScript
const contentString = "Hello, world";
contentString.fixed();
// '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
"".fixed();           // ''
"A < B".fixed();      // 'A < B'
typeof "x".fixed();   // "string"
Try It Yourself

How It Works

Unlike some attribute wrappers, fixed() 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 CSS replacement.

Example 3 — Injecting with innerHTML (Legacy)

How older demos used the returned string — still produces obsolete markup.

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

console.log(html);
// Warning:  is not a valid element in modern HTML
Try It Yourself

How It Works

Even if a browser still renders monospace text for <tt>, the element is obsolete. Use CSS or <code> instead.

Example 4 — Other HTML Wrappers Nearby

Recognize the same pattern family — all deprecated.

JavaScript
"Hi".fixed();   // 'Hi'
"Hi".big();     // 'Hi'
"Hi".bold();    // '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 CSS

MDN’s recommended approach — control the font with font-family.

JavaScript
const el = document.createElement("p");
el.textContent = "Hello, world";
el.style.fontFamily = "monospace";

// document.body.appendChild(el);
console.log(el.style.fontFamily);
console.log(el.outerHTML);

// Semantic alternative for source code:
const code = document.createElement("code");
code.textContent = "const x = 1;";
console.log(code.outerHTML);
Try It Yourself

How It Works

You get a real element, valid markup, and a clear font stack. Prefer a CSS class in production so styles stay maintainable.

🚀 Common Use Cases

  • Reading legacy tutorials — recognize HTML wrapper methods in old samples.
  • Migrating old scripts — replace fixed() with CSS monospace or <code>.
  • Teaching string immutability — show that methods return new strings.
  • Not for new apps — do not use fixed() for monospace UI text.
  • Code snippets — use <code> / <pre> with a monospace font.
  • Safer markup — prefer textContent + CSS over innerHTML strings.

🧠 How fixed() Builds Markup

1

Start with text

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

Input
2

Wrap with tags

Concatenate <tt> + text + </tt>.

Wrap
3

Return an HTML string

No DOM node is created — only text markup.

Result
4

Prefer CSS instead

Set font-family: monospace (or use <code>) on a real element.

📝 Notes

  • fixed() is deprecated — standardized only for compatibility.
  • The <tt> element was removed from the HTML specification.
  • It returns a string, not a DOM node.
  • It takes no parameters.
  • Other HTML wrappers (big(), bold(), fontcolor(), …) share the same fate.
  • Avoid assigning untrusted strings to innerHTML.

Browser & Runtime Support

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

Deprecated · Legacy

String.fixed()

Available in modern browsers for old code, but MDN recommends CSS font-family: monospace 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
fixed() Avoid in new code

Bottom line: Recognize fixed() in legacy samples. For new UI, use CSS monospace fonts or semantic tags like — never rely on the obsolete element from HTML wrapper methods.

Conclusion

String.prototype.fixed() builds an obsolete <tt>...</tt> HTML string for fixed-width text. It is useful for understanding history and migrating old code — not for writing new features.

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

💡 Best Practices

✅ Do

  • Use CSS font-family: monospace for fixed-width text
  • Prefer <code> / <pre> when the text is source code
  • Prefer textContent over string-built HTML
  • Replace fixed() when you touch legacy files
  • Learn wrappers only to recognize deprecated APIs

❌ Don’t

  • Use fixed() in new production code
  • Assume <tt> is valid modern HTML
  • Inject untrusted fixed() output via innerHTML
  • Expect fixed() to return a live element
  • Mix HTML wrappers with modern component frameworks

Key Takeaways

Knowledge Unlocked

Five things to remember about String.fixed()

Deprecated HTML wrapper — prefer CSS monospace.

5
Core concepts
⚠️ 02

Status

deprecated

Legacy
03

Markup

invalid

HTML
04

Replace

monospace

Modern
05

Mutates

no

Immutable

❓ Frequently Asked Questions

String.prototype.fixed() returns an HTML string that wraps the text in a <tt> element — for example "Hello".fixed() returns '<tt>Hello</tt>'. It takes no parameters. Historically <tt> meant a fixed-width (teletype) font.
Yes. MDN marks all HTML wrapper methods as deprecated. The <tt> element itself was removed from the HTML specification. Prefer CSS font-family: monospace (or semantic tags like <code>).
The <tt> element is obsolete. Modern HTML expects monospace styling from CSS, or semantic elements such as <code>, <kbd>, or <samp> when the meaning fits.
No. Strings are immutable. fixed() returns a new string containing HTML markup. The original text stays the same.
Set CSS font-family to a monospace stack, for example element.style.fontFamily = "monospace", or use a CSS class. For inline code, prefer the <code> element.
It remains widely available for compatibility, but you should not use it in new code. Prefer CSS or semantic HTML.
Did you know?

<tt> stood for teletype — a nod to old fixed-width terminals. HTML later favored semantic tags (<code>, <kbd>, <samp>) and left pure presentation to CSS, which is why fixed() is only a compatibility leftover today.

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