It may be too late, but still.

I haven't found popular Java libraries for doing what you want; however, there are many javascript libraries for that (for example, js-beautify). You can save such library source code in resources of your application (you can get the code from one of cdn links, so you don't have to group and minify it manually), and then load it and invoke it with the Nashorn javascript engine.

Your code may look like this (roughly):

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.InputStreamReader;

public class JavascriptBeautifierForJava {

    // my javascript beautifier of choice
    private static final String BEAUTIFY_JS_RESOURCE = "beautify.js";

    // name of beautifier function
    private static final String BEAUTIFY_METHOD_NAME = "js_beautify";

    private final ScriptEngine engine;

    JavascriptBeautifierForJava() throws ScriptException {
        engine = new ScriptEngineManager().getEngineByName("nashorn");

        // this is needed to make self invoking function modules work
        // otherwise you won't be able to invoke your function
        engine.eval("var global = this;");
        engine.eval(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(BEAUTIFY_JS_RESOURCE)));
    }

    public String beautify(String javascriptCode) throws ScriptException, NoSuchMethodException {
        return (String) ((Invocable) engine).invokeFunction(BEAUTIFY_METHOD_NAME, javascriptCode);
    }

    public static void main(String[] args) throws ScriptException, NoSuchMethodException {
        String unformattedJs = "var a = 1; b = 2; var user = { name : \n \"Andrew\"}";

        JavascriptBeautifierForJava javascriptBeautifierForJava = new JavascriptBeautifierForJava();
        String formattedJs = javascriptBeautifierForJava.beautify(unformattedJs);

        System.out.println(formattedJs);
        // will print out:
        //        var a = 1;
        //        b = 2;
        //        var user = {
        //            name: "Andrew"
        //        }
    }
}

If you're going to use this approach, make sure to reuse JavascriptBeautifier object, because it's not too effective to recreate one whenever you need to beautify code.

Answer from Roman Golyshev on Stack Overflow
๐ŸŒ
Beautifier
beautifier.io
Online JavaScript beautifier
Sublime Text 2: JsFormat, a javascript formatting plugin for this nice editor by Davis Clark,
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ jsbeautifier
Best JSBeautifier to beautify / format JavaScript
This Javascript Beautifier that supports indentation levels: 2 tab spaces, 3 tab spaces, and 4 tab spaces. Download JS, once it's formatted, created or modified and this js file can be opened in Sublime, VSCode, and Notepad++ alternative.
Discussions

Java: format Javascript code - Stack Overflow
In my architecture Javascript code is generated in the server. A huge chunk of Javascript code is generated, stored in a java.lang.String and sent to the client side. I want to more easily debug the More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - How to format a JS script? - Stack Overflow
I have a simple question I am an ethical hacker and I want to see and discover a fake website. I want to see a JS script from an HTML usually this script is externally put from some website. like More on stackoverflow.com
๐ŸŒ stackoverflow.com
JavaScript equivalent to printf/String.Format - Stack Overflow
I'm looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .NET). My basic requirement is a thousand separator format for n... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c# - Use of String.Format in JavaScript? - Stack Overflow
This is driving me nuts. I believe I asked this exact same question, but I can't find it any more (I used Stack Overflow search, Google Search, manually searched my posts, and searched my code... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Prettier
prettier.io
Prettier ยท Opinionated Code Formatter ยท Prettier
Your code is formatted on save ยท No need to discuss style in code review ยท Saves you time and energy ยท And more ยป ยท JavaScript ยท JSX ยท Flow ยท TypeScript ยท JSON ยท CSS ยท Less ยท SCSS ยท styled-components ๐Ÿ’… ยท styled-jsx ยท HTML ยท Vue ยท Angular ยท Ember / Handlebars ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ utilities โ€บ javascript-formatter
Online Free JavaScript Formatter and Beautifier - GeeksforGeeks
1 week ago - Step 1. Input the JavaScript code in the first box. You can write the code, copy and paste it, or upload the JavaScript code file. Step 2. Click on the format button to format JavaScript code.
Top answer
1 of 5
5

It may be too late, but still.

I haven't found popular Java libraries for doing what you want; however, there are many javascript libraries for that (for example, js-beautify). You can save such library source code in resources of your application (you can get the code from one of cdn links, so you don't have to group and minify it manually), and then load it and invoke it with the Nashorn javascript engine.

Your code may look like this (roughly):

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.InputStreamReader;

public class JavascriptBeautifierForJava {

    // my javascript beautifier of choice
    private static final String BEAUTIFY_JS_RESOURCE = "beautify.js";

    // name of beautifier function
    private static final String BEAUTIFY_METHOD_NAME = "js_beautify";

    private final ScriptEngine engine;

    JavascriptBeautifierForJava() throws ScriptException {
        engine = new ScriptEngineManager().getEngineByName("nashorn");

        // this is needed to make self invoking function modules work
        // otherwise you won't be able to invoke your function
        engine.eval("var global = this;");
        engine.eval(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(BEAUTIFY_JS_RESOURCE)));
    }

    public String beautify(String javascriptCode) throws ScriptException, NoSuchMethodException {
        return (String) ((Invocable) engine).invokeFunction(BEAUTIFY_METHOD_NAME, javascriptCode);
    }

    public static void main(String[] args) throws ScriptException, NoSuchMethodException {
        String unformattedJs = "var a = 1; b = 2; var user = { name : \n \"Andrew\"}";

        JavascriptBeautifierForJava javascriptBeautifierForJava = new JavascriptBeautifierForJava();
        String formattedJs = javascriptBeautifierForJava.beautify(unformattedJs);

        System.out.println(formattedJs);
        // will print out:
        //        var a = 1;
        //        b = 2;
        //        var user = {
        //            name: "Andrew"
        //        }
    }
}

If you're going to use this approach, make sure to reuse JavascriptBeautifier object, because it's not too effective to recreate one whenever you need to beautify code.

2 of 5
3

Two closely-related questions (possible duplicates):

  • Command line JavaScript code beautifier that works on Windows and Linux
  • https://stackoverflow.com/questions/6260431/pretty-print-javascript-using-java
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Intl โ€บ NumberFormat
Intl.NumberFormat - JavaScript | MDN
Getter function that formats a range of numbers according to the locale and formatting options of the Intl.NumberFormat object from which the method is called.
Find elsewhere
๐ŸŒ
FreeFormatter
freeformatter.com โ€บ javascript-beautifier.html
Free Online Javascript Beautifier / Formatter - FreeFormatter.com
Formats JavaScript files with the chosen indentation level and your choice of braces. Supports 4 indentation levels: 2 spaces, 3 spaces, 4 spaces and tabs.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ jsviewer
Best Javascript Beautifier tool work as JavaScript Formatter, Viewer and Prettier
Often the javascript used is provided has white space compressed to reduce the size of the data transferred. This site give you a quick and easy way to format (beautifier) the javascript so you can easily read it.
Top answer
1 of 16
1662

Current JavaScript

From ES6 on you could use template strings:

let soMany = 10;
console.log(`This is ${soMany} times easier!`);
// "This is 10 times easier!"

See Kim's answer below for details.


Older answer

Try sprintf() for JavaScript.


If you really want to do a simple format method on your own, donโ€™t do the replacements successively but do them simultaneously.

Because most of the other proposals that are mentioned fail when a replace string of previous replacement does also contain a format sequence like this:

"{0}{1}".format("{1}", "{0}")

Normally you would expect the output to be {1}{0} but the actual output is {1}{1}. So do a simultaneous replacement instead like in fearphageโ€™s suggestion.

2 of 16
1512

Building on the previously suggested solutions:

// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
  String.prototype.format = function() {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number]
        : match
      ;
    });
  };
}

"{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET")

outputs

ASP is dead, but ASP.NET is alive! ASP {2}


If you prefer not to modify String's prototype:

if (!String.format) {
  String.format = function(format) {
    var args = Array.prototype.slice.call(arguments, 1);
    return format.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number] 
        : match
      ;
    });
  };
}

Gives you the much more familiar:

String.format('{0} is dead, but {1} is alive! {0} {2}', 'ASP', 'ASP.NET');

with the same result:

ASP is dead, but ASP.NET is alive! ASP {2}

๐ŸŒ
Static.app
static.app โ€บ js-formatter
JS Formatter - Static.app
Whether using a desktop, tablet, or smartphone, our JS Formatter is fully responsive and accessible from any device. You can format your JavaScript files on the go, ensuring that your code is always clean and organized, no matter where you are.
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ how to format dates in javascript: methods, libraries, and best practices
How to format dates in JavaScript: Methods, libraries, and best practices - LogRocket Blog
May 8, 2025 - While Moment.js was once the go-to library for date handling in JavaScript, it is now considered legacy. The Moment.js team has officially declared the library in maintenance mode and recommends newer alternatives. That said, many existing projects still use it, so itโ€™s worth understanding its approach. ... Letโ€™s look at a typical Moment.js workflow: creating date objects from strings or custom formats, formatting them into readable outputs, adjusting dates by adding or subtracting time, and converting them to specific time zones.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Intl โ€บ NumberFormat โ€บ format
Intl.NumberFormat.prototype.format() - JavaScript | MDN
July 10, 2025 - The format() method of Intl.NumberFormat instances formats a number according to the locale and formatting options of this Intl.NumberFormat object.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-format-a-date-with-javascript-date-formatting-in-js
How to Format a Date with JavaScript โ€“ Date Formatting in JS
November 7, 2024 - This is why we need to format dates so we can extract what we need from this date object. In JavaScript, there is no direct syntax that provides you with your expected format because date format varies based on location, circumstance, and so on.
๐ŸŒ
DebugBear
debugbear.com โ€บ tool โ€บ beautify-js
Online JavaScript Beautifier: Format JS Code
Prettify JavaScript code to make it easier to read and analyze minified JavaScript files.
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ javascript-date-format
How to Format a Date in JavaScript | Codecademy
In JavaScript, the Date() constructor returns a new Date object representing the current date or a specific date. If we create a Date object without any input parameters, it contains the current date and time along with the timezone details.
๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ js-formatting-date
How to Format Dates in JavaScript | Built In
November 12, 2024 - JavaScript offers a variety of ways to format dates, including using the date object, intl.DateTimeFormat and a number of date libraries. Hereโ€™s how to use each one.
Top answer
1 of 16
74

Adapt the code from MsAjax string.

Just remove all of the _validateParams code and you are most of the way to a full fledged .NET string class in JavaScript.

Okay, I liberated the msajax string class, removing all the msajax dependencies. It Works great, just like the .NET string class, including trim functions, endsWith/startsWith, etc.

P.S. - I left all of the Visual Studio JavaScript IntelliSense helpers and XmlDocs in place. They are innocuous if you don't use Visual Studio, but you can remove them if you like.

<script src="script/string.js" type="text/javascript"></script>
<script type="text/javascript">
    var a = String.format("Hello {0}!", "world");
    alert(a);

</script>

String.js

// String.js - liberated from MicrosoftAjax.js on 03/28/10 by Sky Sanders
// permalink: http://stackoverflow.com/a/2534834/2343

/*
    Copyright (c) 2009, CodePlex Foundation
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are permitted
    provided that the following conditions are met:

    *   Redistributions of source code must retain the above copyright notice, this list of conditions
        and the following disclaimer.

    *   Redistributions in binary form must reproduce the above copyright notice, this list of conditions
        and the following disclaimer in the documentation and/or other materials provided with the distribution.

    *   Neither the name of CodePlex Foundation nor the names of its contributors may be used to endorse or
        promote products derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
    IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</textarea>
*/

(function(window) {

    $type = String;
    $type.__typeName = 'String';
    $type.__class = true;

    $prototype = $type.prototype;
    $prototype.endsWith = function String$endsWith(suffix) {
        /// <summary>Determines whether the end of this instance matches the specified string.</summary>
        /// <param name="suffix" type="String">A string to compare to.</param>
        /// <returns type="Boolean">true if suffix matches the end of this instance; otherwise, false.</returns>
        return (this.substr(this.length - suffix.length) === suffix);
    }

    $prototype.startsWith = function String$startsWith(prefix) {
        /// <summary >Determines whether the beginning of this instance matches the specified string.</summary>
        /// <param name="prefix" type="String">The String to compare.</param>
        /// <returns type="Boolean">true if prefix matches the beginning of this string; otherwise, false.</returns>
        return (this.substr(0, prefix.length) === prefix);
    }

    $prototype.trim = function String$trim() {
        /// <summary >Removes all leading and trailing white-space characters from the current String object.</summary>
        /// <returns type="String">The string that remains after all white-space characters are removed from the start and end of the current String object.</returns>
        return this.replace(/^\s+|\s+$/g, '');
    }

    $prototype.trimEnd = function String$trimEnd() {
        /// <summary >Removes all trailing white spaces from the current String object.</summary>
        /// <returns type="String">The string that remains after all white-space characters are removed from the end of the current String object.</returns>
        return this.replace(/\s+$/, '');
    }

    $prototype.trimStart = function String$trimStart() {
        /// <summary >Removes all leading white spaces from the current String object.</summary>
        /// <returns type="String">The string that remains after all white-space characters are removed from the start of the current String object.</returns>
        return this.replace(/^\s+/, '');
    }

    $type.format = function String$format(format, args) {
        /// <summary>Replaces the format items in a specified String with the text equivalents of the values of   corresponding object instances. The invariant culture will be used to format dates and numbers.</summary>
        /// <param name="format" type="String">A format string.</param>
        /// <param name="args" parameterArray="true" mayBeNull="true">The objects to format.</param>
        /// <returns type="String">A copy of format in which the format items have been replaced by the   string equivalent of the corresponding instances of object arguments.</returns>
        return String._toFormattedString(false, arguments);
    }

    $type._toFormattedString = function String$_toFormattedString(useLocale, args) {
        var result = '';
        var format = args[0];

        for (var i = 0; ; ) {
            // Find the next opening or closing brace
            var open = format.indexOf('{', i);
            var close = format.indexOf('}', i);
            if ((open < 0) && (close < 0)) {
                // Not found: copy the end of the string and break
                result += format.slice(i);
                break;
            }
            if ((close > 0) && ((close < open) || (open < 0))) {

                if (format.charAt(close + 1) !== '}') {
                    throw new Error('format stringFormatBraceMismatch');
                }

                result += format.slice(i, close + 1);
                i = close + 2;
                continue;
            }

            // Copy the string before the brace
            result += format.slice(i, open);
            i = open + 1;

            // Check for double braces (which display as one and are not arguments)
            if (format.charAt(i) === '{') {
                result += '{';
                i++;
                continue;
            }

            if (close < 0) throw new Error('format stringFormatBraceMismatch');


            // Find the closing brace

            // Get the string between the braces, and split it around the ':' (if any)
            var brace = format.substring(i, close);
            var colonIndex = brace.indexOf(':');
            var argNumber = parseInt((colonIndex < 0) ? brace : brace.substring(0, colonIndex), 10) + 1;

            if (isNaN(argNumber)) throw new Error('format stringFormatInvalid');

            var argFormat = (colonIndex < 0) ? '' : brace.substring(colonIndex + 1);

            var arg = args[argNumber];
            if (typeof (arg) === "undefined" || arg === null) {
                arg = '';
            }

            // If it has a toFormattedString method, call it.  Otherwise, call toString()
            if (arg.toFormattedString) {
                result += arg.toFormattedString(argFormat);
            }
            else if (useLocale && arg.localeFormat) {
                result += arg.localeFormat(argFormat);
            }
            else if (arg.format) {
                result += arg.format(argFormat);
            }
            else
                result += arg.toString();

            i = close + 1;
        }

        return result;
    }

})(window);
2 of 16
45

Here is what I use. I have this function defined in a utility file:

  String.format = function() {
      var s = arguments[0];
      for (var i = 0; i < arguments.length - 1; i++) {       
          var reg = new RegExp("\\{" + i + "\\}", "gm");             
          s = s.replace(reg, arguments[i + 1]);
      }
      return s;
  }

And I call it like so:

var greeting = String.format("Hi, {0}", name);

I do not recall where I found this, but it has been very useful to me. I like it because the syntax is the same as the C# version.