Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why do some method do not have key and the function keyword.

<script>
 let Greet= {
 "name":"Jogn",
 sayHi(){
 alert('HI')

 }
 }
 Greet.sayHi()
</script>

1 Answer

Because in this case the “name” is the key and “John” is the value. Key is usually used for loops so you know what’s being returned. But you can call it anything.
The function doesn’t have the function keyword as in this case is inside a variable and is being defined as an arrow function just like ()=> alert(“Hi”). In this case you need to give it a name so you can call it using the Greet.sayHi. Otherwise anytime you call Greet the function will run.

I may be wrong since I haven’t used JavaScript in a while but I far as I can remembers that the theory