Saurabh

Learn Functions Changing Non-Primitive Properties from an online marketing company

Non-Primitive Properties

Another thing I would like to point out when it comes to functions is how the values you pass in are affected by the function itself. So you know what let me raise some code here and what I mean by that is if we pass in a primitive parameter, for example, a number that's passed in by what's called value. So the value is passed into the function. If the function changes the value of that parameter it does not affect the best calling online marketing company and its function. In other words, let's say we have a let my number equals for example and we have a function that takes in that number and changes it somehow. So, for example, let function or function add takes in a number and it returns no plus. Pretty straightforward stuff here. So if you cancel a log at with my number in it's run it. The original number is not affected.

So if we look at my online marketing company at this point clear we'll run didn't quite catch it. We'll try that again still no. Try that again. Save it here. See if it runs. There you go. OK. So we saw that the original number was added. The was added to the original number and the original number is not affected right we're passing it value but the original one is not affected. Same goes for something like a string. So I say let my string equals string and then we return that. So let's see. Hello for example to whatever string is passed in so return Hello. Plus my string or string actually what we pass in here at hello to my string it's clear it will run it no space but I think you get the idea about the online marketing company that we're not affecting the original string.

Functions Changing

We console log that out to double check create a sanity check. Never heard anybody clear run. Registering is not effective if we pass in a different type of thing. In other words a non-permitted value like an array or an object that could have some effect on the original item. So definitely something to be aware of. So as an example let's say you know we're doing our best to make it something immutable. So let's say we have a concept my info and it's an object is of course you know you can make key-value pairs. It could say name double quotes name Chris age for example. Right, so this is an object. Now let's make a function that changes the age of an online marketing company, for example, some change. Age takes an object. My object my object. And what this does is it will take my objects and B want to set the value of something in an object which we can do by accessing the key name essentially.

So my object at age equals for example. So the entire purpose of this function is to change the age to. All right. So let's check it out. So a console logs my info. Which of course will be pretty straightforward. It's an object age named Chris. What about if we invoke the function of change age to pass in my online marketing company info and then we check again console log. My infelicities going to copy and paste it. That's all clear. We're running it again. And oh no I have changed the original object right. Again it's a constant. So you may think it's mutable but again that's just referring to the actual object itself. The stuff inside the object can be changed. Right so depending on what you want to do this may or may not be the behavior you want. In this case, I have changed my original info. Maybe I didn't intend to. So that's something you definitely want to know when it comes to functions. They can change things that sometimes maybe they shouldn't be able to change or maybe was contrary to your intention and the same could happen with an array as well.

For example, Const my numbers equal for example is an array right it's a collection of data. Function ad adds to the array, for example, takes in my array and what this does it takes my array and the way to an array of course. You can push objects into the array and we can push in the number for example. So again if we cancel log my numbers. I think you kind of get the idea at this point right so my numbers or. What if we invoke the function add to array pass in my numbers array and we cancel log my numbers again. The clear run original online marketing company has been changed slightly. Even Again it's a cost which is referring to the array itself not the data inside the array. So you know not hugely important at this point but definitely something you're going to want to know about functions going forward.

  • Love
  • Save
    Add a blog to Bloglovin’
    Enter the full blog address (e.g. https://www.fashionsquad.com)
    We're working on your request. This will take just a minute...