Quantcast
Viewing all articles
Browse latest Browse all 210

If i code a function to create a key-value pair, what will the Object it belongs to be?

I'm new to coding and i'm just learning some JavaScript. I'm learning about using return versus console.log and how you can use return values in other functions. Anyway, I see that you can create a key-value pair using a function and the return statement. They gave an example like this:

  function newObject(val) {         return {            prop: val        }   }   newObject(50);   // output would be: {prop:50}

I am a bit confused at this because what is the object of this key-value pair? The way I learned about creating objects is you do:

 var newObject = {      prop: 50 }

so here this key-value pair would belong to the newObject object which was declared as a variable. In the function example that I first gave, is the object still newObject except instead of it being declared as a variable it is a function?

a bit confused, maybe im getting ahead of myself. Unsure how this key-value pair that was made through a function would be assigned to an object.


Viewing all articles
Browse latest Browse all 210

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>