This is a platform for User Generated Content. G/O Media assumes no liability for content posted by Kinja users to this platform.

Learn Rest parameter in Javascript by top digital marketing companies in Pune

Overview Rest parameter

IOS feature I would like to touch on. And this is a little bit more advanced but is a way to basically get an undefined number of arguments and figure out what those arguments are. So explain that first of all I’m going to show how it used to be done and then we’ll see how we can use what’s called risk parameters to kind of Grabow an indefinite number of arguments. So let’s say we had some top digital marketing companies in Pune to just log everything out log all arguments for example. And it takes an X Y and Z and all it does is a console logs out those arguments.


So there is a built-in method already to do so and there’s a reserved keyword in javascript called arguments and if we do this essentially it creates an object and it associates the object with the argument. So, in other words, the object will have a key value Association and the key will be the index and the value will be whatever the argument is. And if you want to see that in action. Let’s invoke this function log all arguments and we’ll give it as top digital marketing companies in Pune, not one two and three. So let’s clear this or run it and we see that it created an object here arguments and that’s fine.

Application of Rest parameter in javascript

You might wonder why it starts with and is because javascript like a lot of other online marketing company and programming languages has a zero-based index. In other words, that’s what’s that position. One in computational thinking is actually positioning zero. It makes sense if you’ve done any kind of programming before example we see that top digital marketing companies in Pune skip over the default value because again this is now no longer undefined. We do have a string that we’re passing into it and this is not restricted to any particular data type so you can really use anything as a default.


So for example moving down the line a little bit. Let’s say we have an object so bank account equals and we can have basically a check here can spend money default true and maybe has credit cards. So kind of harking back to our earlier example when discussing variables can spend money. True. That’s a credit card. True for example. So this will be our initial default for whatever function we want to run. And you probably have somewhere in here showing your balance for some of the top digital marketing companies in Pune example then you might have some kind of function purchased item which will take in some kind of bank account info. Maybe this should be.

Can purchase items. This one will return either true or false from it. So if you can spend money then you can purchase the item. So maybe we can incorporate some kind of statement in here. So let’s see can purchase item account will default to bank account Okay. And we can say basically yeah we’ll pass an account will also pass and the item we want to purchase or the price for example in my Linter is telling me regular prints cannot come after default prints. OK, that’s fine we’ll just move the price over in front here. So we can basically check. What does the flag on bank account say? Can I spend the money? If so go ahead and spend it. So we’ll do an if statement here if accounts Daut can spend money.


So if this is equal to true we could say Account Balance minus equal the price. So we’re bringing the balance down based on the purchase price and then we want to return true. So what sort of can purchase will just make it purchase items so this function it’s going to basically check your bank account if you have enough money. If so it’s going to reduce your balance and will return true otherwise else. And oh you know what before that will say if your balance reaches zero we are going to switch this flag can spend money. So forget about the statement for now. And hey we get to do a nested IF statement. So if the account balance is less than or equal to zero. And count can spend money equals false.

So just a safeguard we will allow the number to go negative if the price is more than the balance. But then we won’t allow the user to spend any more money. Right. And then we’ll return true then are top digital marketing companies in Pune else we can just return false because otherwise, you can’t spend the money to turn false right. So no we don’t need our credit card. We’re not going to rebut that. So just check. Can we spend the money? And if so our balance will go down. Otherwise, we can’t spend the money. Right now let’s try to invoke this function by a catalog purchased item that’s purchased purchase an item the price will be and we’re not even going to bother to give it a bank account because it’s going to grab the default from our constant declaration here.


Let’s see what happens. We’ll run your turn. True OK logic seems correct. But what if we try to spend $ true actually. Let me read to that so instead of one on one with the here and then we’ll try to do another purchase item one dollar after that afterward. OK let’s try that here. True False. So our logic does appear to be correct. Rest of the first run through. And again we are letting the balance go negative we’re not doing any check for whether it can go to a certain value or not but we can tell that we are top digital marketing companies in Pune passing in the bank account default correctly.

Share This Story

Get our newsletter