DEV Community

loading...
Cover image for TIL About .bold(), .blink(), and More JavaScript String Methods

TIL About .bold(), .blink(), and More JavaScript String Methods

bendmyers profile image Ben Myers ・2 min read

While playing around in the Chrome DevTools, I noticed a string method I had never seen before: .bold(). Curiosity got the better of me, and I gave it a try.

let myString = 'Hello World!';
myString.bold(); // returns "<b>Hello World!</b>"

It's a method that returns the string wrapped in a <b> tag. It's very deprecated, but all major browsers still support it.

.bold() is not alone - I've found several other string methods that are in the same boat. Again, these are all deprecated, and should not be used in the wild.

  • .anchor(name): Returns the string wrapped in <a> tags with the name attribute set to the given name
  • .big(): Returns the string wrapped in <big> tags.
  • .fixed(): Returns the string wrapped in <tt> tags.
  • .fontcolor(color): Returns the string wrapped in <font> tags with the color attribute set to the given color
  • .fontsize(size): Returns the string wrapped in <font> tags with the size attribute set to the given size
  • .italics(): Returns the string wrapped in <i> tags.
  • .link(url): Returns the string wrapped in <a> tags with the href attribute set to the given URL
  • .small(): Returns the string wrapped in <small> tags.
  • .strike(): Returns the string wrapped in <strike> tags.
  • .sub(): Returns the string wrapped in <sub> tags.
  • .sup(): Returns the string wrapped in <sup> tags.

... and, of course, my personal favorite: .blink(), which returns your string wrapped in a <blink>.

As with .bold(), these methods are all officially deprecated. Several of them even wrap your strings in tags that have long since been deprecated. These methods remain in browsers for the sake of backwards compatibility, an artifact of development past.

Discussion (8)

pic
Editor guide
Collapse
michelc profile image
Michel • Edited

The String object has so many methods: developer.mozilla.org/en-US/docs/W....

Maybe it would be better to emphasize "these methods are all officially deprecated" and write it in bold...

Collapse
bendmyers profile image
Ben Myers Author

I appreciate the feedback, and you're absolutely right. I've gone ahead and edited in a more prominent disclaimer. Thank you!

Collapse
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

See, this is the kind of JavaScript content that Dev.to needs. Not another "this in JavaScript` or "How Array.reduce works" repost.

Collapse
helleworld_ profile image
DesirΓ© πŸ‘©β€πŸŽ“πŸ‘©β€πŸ«

OKAY what!

I've been with JS for many years and didn't had a single clue about this, how GREAT! Now I think of it, if we had functions for numbers why wouldn't they exist for strings too, right? haha πŸ˜†

Thank you so much, Ben!

Collapse
dexygen profile image
George Jempty • Edited

I think you might find this interesting too if you don't already know it. Let's say you have an element in your page with the id of "foobar" -- well then there will be a global variable named "foobar" that points to this element.

Collapse
rubenofen profile image
rubenofen

I'm going to use one of these today in my code :D :D

Collapse
bendmyers profile image
Collapse
mzaini30 profile image
Zen

Wow I just found out those tricks.