Search code examples
javascriptunicodefonts

How to console.log() , any letter but in bold?


There are certain ways to print letters in javascript using unicode FOr example

console.log(\u0062) yields a b , so its the same as putting console.log(b)

But now, I do know there are certains way to console.log bold letters, for example

console.log('𝗯𝗼𝗹𝗱')

prints a bold word in the console.

But what would be a function for example to print in bold?

THis is my idea

const printBold = (word: string) => {
  //splits the string, gets every letter, and replaces with the bold unicode equivalent
} 

But... im unable to find a unicode equivalent for bold letters, or a way to create a function that logs in bold.

Maybe I can do something like

const map = {
 "a": "bold_a",
 "b": '𝗯'
}

And feed the converter with that data, but... where do I get the bold unicodes or characters from? THe only ones I could find, i copied them from this answer https://stackoverflow.com/a/70569635/9010895


Solution

  • Your current approach is seriously flawed. Those are not bold letters, they are mathematic symbols designed for writing equations. Trying to use them as bold letters will play havock with screen readers.

    The console does have features for formatting text which you should use instead.

    You can use a format specifier:

    const bold = "font-weight: bold";
    const normal = "font-weight: normal";
    console.log("A string with a %cbold%c word", bold, normal);
    

    Or an ANSI escape sequence