Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:51:16+00:00 2026-06-15T22:51:16+00:00

JS newbie question here: Let’s say I have some simple methods for string manipulation,

  • 0

JS newbie question here:

Let’s say I have some simple methods for string manipulation, like these silly examples:

var prepend = function(str) {
  return 'foo ' + str
};

var exclaim = function(str) {
  return str + '!'
}

Now, I can use these like so:

var string = prepend('bar'); // 'foo bar'
string = exclaim(string); // 'foo bar!'

However, I think it would be really neat if I could also, optionally, make these methods chainable, so I could also do something like:

var string = "foo"
string.prepend().exclaim()

… and I’d still get "foo bar!" as my result.

It would be even better if I could also pass optional args into this chain, like:

var exclaim = function(string,char="!") {
  return string + char
};

So, my questions are:

  1. So basic, but am I describing this correctly? What do you call a method that can be chained like this? And, what do you call the return of the previous method in the chain, the thing that this next method will operate on?

  2. How do you set a variable to be either the return of the previous method in the chain, or else provided as an argument?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:51:17+00:00Added an answer on June 15, 2026 at 10:51 pm

    If you want to chain them, then you’ll have to add these methods to the string prototype:

    String.prototype.prepend = function() {
      return 'foo ' + this
    };
    
    String.prototype.exclaim = function() {
      return this + '!'
    }
    
    var s = "".prepend().exclaim();
    console.log(s);
    

    DEMO


    WRT optional parameters, you can take advantage of the fact that in JavaScript, you can call a method without passing values for some of the parameters. Those un-passed parameters will show up as undefined in the function. Since null implicitly converts to undefined, that’s often handled by checking those parameters for null

    var exclaim = function(str, ch) {
      if (ch == null){
          ch = '!';
      }
      return string + ch
    };
    

    You’ll often also see something like this:

    var exclaim = function(str, ch) {
      ch = ch || '!';
      return string + ch
    };
    

    That’s similar, but will overwrite any of the 6 “falsy” values: null, undefined, NaN, 0, false and '' (empty string). So if you were to do that, and try to pass an empty string in for ch, it would be overwritten.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a newbie question about EF4. Let's say I have in my EDMX
Newbie question here but for some reason I cant figure this out. I have
Just a quick newbie question here. I have a method that calculates a value
A really php newbie here, sorry if the question is too simple. I’m following
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =
Newbie question here. I have a web form with a text area and naturally
SSRS newbie question here... I have a table where one column is varbinary(max) data.
newbie question here: I have FreeTDS working and querying a Microsoft SQL server 2008
Newbie question here! I'm building a simple application that allows users to create and
Newbie question here. I'd like to be able to specify through data (i.e. an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.