String formatting in JavaScript: analog of printf or String.Format
Hello, colleagues! I'm new to JavaScript, coming from Python and C#. There I was used to convenient string formatting: in Python — f"Hello, {name}", in C# — String.Format("Hello, {0}", name). But in JavaScript I only see concatenation with plus signs: "Hello, " + name + "! You have " + count + " messages." — this looks terrible, especially when there are many variables.
I know about template literals with backticks: `Hello, ${name}!` But is there a built-in function in JavaScript similar to printf from C or String.Format from .NET? For example, to be able to write something like:
let result = sprintf("Hello, %s! You have %d new messages.", name, count);
Or:
let result = String.Format("PI value = {0:F2}", Math.PI);
I tried searching, but didn't find anything like this in the documentation.
No answers yet.
Log in to leave an answer
Blogs