Click here to Skip to main content
13,051,413 members (62,036 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 20 May 2014

String formatting in JavaScript

, 20 May 2014
Rate this:
Please Sign up or sign in to vote.
String.Format creates strings from a pattern and values.


Replaces each format items in a specified string with the text equivalent of a corresponding parameters value. One or more format items in a specified string with the string representation of a specified parameter value.


'string with format{0}'.format(Object arg0, Object arg1, Object argn);

{0} : type of string object

arg0 : The first object to format.

arg1 : The second object to format.

argn : The Nth object to format.

Using the code

String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
        if (m == "{{") { return "{"; }
        if (m == "}}") { return "}"; }
        return args[n];


This example shows the use of the string.Format method to combine three strings with formatting options. The format string itself is the first argument to the string.Format method and it is specified as a string literal.

The "{0}", "{1}", "{2}" and "{N}" indicate where the first, second, third and Nth arguments are inserted in to the string object. The parameter should be between the { } brackets.

"Hello {0}.{1}, Welcome to our new house at {2}.".format('Mr','Imdadhusen','Ahmedabad');


Hello Mr.Imdadhusen, Welcome to our new house at Ahmedabad.

More Examples

var startDate = "21 APR 2014";
var endDate = "24 APR 2014";
"Your score is {0} out of {1}".format(175,250);
"Dear {0}, Your ticket is booked for {1} days from {2} to {3}. Thank you for booking from {4}".format('Imdadhusen',4,startDate, endDate, '');
"World T{0} - {1}th match, Group {2}, {3} v {4}, {3} won by {5} wickets (with {6} balls remaining).  {4}'s next match will be on {7}.".format(20,13,2,'India','Pakistan', 7, 9, '25 Apr 2014');
Your score is 175 out of 250 

Dear Imdadhusen, Your ticket is booked for 4 days from 21 APR 2014 to 24 APR 2014. Thank you for booking from

World T20 - 13th match, Group 2, India v Pakistan, India won by 7 wickets (with 9 balls remaining). Pakistan's next match will be on 25 Apr 2014.

Points of Interest

Using the above string utility you can pass n number of parameters with repetitive parameter to generate complete string.

Please fill free to ask me if you would require any help for the same.

Your valuable feedback, comment, suggestion are highly appreciated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sunasara Imdadhusen
Software Developer (Senior) Infostretch Ahmedabad-Gujarat
India India
Aspiring for a challenging carrier wherein I can learn, grow, expand and share my existing knowledge in meaningful and coherent way.

sunaSaRa Imdadhusen


  1. 2nd Best Mobile Article of January 2015

  2. 3rd Best Web Dev Article of May 2014

  3. 2nd Best Asp.Net article of MAY 2011

  4. 1st Best Asp.Net article of SEP 2010

Read More Articles...

You may also be interested in...

Comments and Discussions

Questionsimple and easy to learn Pin
Jcmorin21-May-14 6:33
memberJcmorin21-May-14 6:33 
AnswerRe: simple and easy to learn Pin
Sunasara Imdadhusen21-May-14 18:29
professionalSunasara Imdadhusen21-May-14 18:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 20 May 2014
Article Copyright 2014 by Sunasara Imdadhusen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid