Object Literal in Javascript – How to Create (and Use) an Object and its Properties?

A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces. Object literals encapsulate data, enclosing it in a tidy package.

comma , is needed at the end of property declaration.

A trailing comma is a comma for your last property. There is no error generated for not putting a comma for your last property, but it is considered a good practice – to anticipate a potential syntactical error due to missing a comma when a new property added in the future.

Semicolon ; is placed at the end of an object declaration, assignment and a return statement.

Without a semicolon, the code will be considered “undone”. So a statement will not be a statement, an assignment will not assign anything, a declaration will not declare anything – UNLESS your program uses a transpiler such as Babel, which will allow you to omit semicolons. If you don’t transpile/uglify and lint your code properly then I do not recommend you omit semicolons in your code.

Example of object literal in Javascript:

let menu = {
   property : value,
   property : value,
   property: value, // this is called a trailing comma
}; 

Now, sometimes … a property can also be an object of its own, like :

let menu = {
  
   propertyObject : { 
      propertyOfThisObject : value,
      propertyOfThisObject : value,
   },
   
   property : value,
   property: value,
}; 

Sometimes, a function can also be the property which will practically become your class method too, like :

let menu = {
   propertyObject : { 
      propertyOfThisObject : value,
      propertyOfThisObject : value,
   },
   property : value,
   
   // below is a function as a property
   functionNameAsKey : function(item) {
        x = item; 
        return x; 
   }, 
   // below is function property in its shorter syntax form
   functionNameAsKey(item) {
        x = item; 
        return x; 
   }, 
}; 

How to call a property so we can retrieve the value?

menu.property;

If the property is a function, you call it by running it with (), like so :

menu.functionNameAsKey();

When you mention a function property without (), like :

menu.functionNameAsKey; 

That means you don’t run/call it and all it will return is just the reference to the function – which could be intentional.

Usually we need the reference to the function so we could store the function value in a variable, like so :

let func = menu.functionNameAsKey;

Or we store the function reference on an event handler (onclickonchange) of an element like document or button, and the function will run only when the event fires. But until then, the function is just sitting quietly in its storage.

document.onload = menu.functionNameAsKey;

button.onclick = menu.functionNameAsKey; 

$("button").click(menu.functionNameAsKey); // jQuery

57 Comments

  1. phone interview questions July 20, 2020 at 11:43 am

    https://waterfallmagazine.com
    Thanks for ones marvelous posting! I quite enjoyed reading it, you might be a great author.
    I will remember to bookmark your blog and definitely will come back down the road.
    I want to encourage one to continue your great work, have a nice morning!

  2. Wieliczko.eu July 31, 2020 at 8:38 am

    http://wieliczko.eu/ To change the place of photos or text
    on the web page, you have to drag the photo box with your computer mouse.

  3. 우리카지노 November 22, 2020 at 2:53 pm

    I was suggested this blog through my cousin. I am now not sure whether this post is written through him as no one
    else recognize such designated about my trouble. You’re wonderful!
    Thank you!

  4. film modu December 10, 2020 at 4:37 pm

    I think you have mentioned some very interesting details, regards for the post. Melisande Maurits Gypsie

  5. erotik izle December 14, 2020 at 9:02 pm

    I quite like reading a post that can make people think. Also, thank you for allowing for me to comment. Caterina Raviv Tuchman

  6. canli tv December 17, 2020 at 8:44 am

    What a material of un-ambiguity and preserveness of precious knowledge concerning unexpected feelings. Cary Colman Mientao

  7. erotik December 17, 2020 at 9:58 pm

    What a material of un-ambiguity and preserveness of valuable familiarity about unexpected feelings. Mechelle Delmer Ehrsam

  8. sikis December 18, 2020 at 4:44 am

    Really informative blog article. Thanks Again. Really Great. Marcelline See Jerrold

  9. erotik December 18, 2020 at 8:21 am

    This design is wicked! You obviously know how to keep a reader amused. Joanne Rodolph Landmeier

  10. bahis December 18, 2020 at 10:09 am

    Very informative post. Much thanks again. Really Great. Deanne Miguel Artemus

  11. sikis December 18, 2020 at 12:31 pm

    I am regular visitor, how are you everybody? This piece of writing posted at this web page is truly good. Abigail Ollie Vally

  12. erotik December 18, 2020 at 2:16 pm

    At this time it sounds like Movable Type is the top blogging platform out there right now. Ruthi Redd Lincoln

  13. sikis December 18, 2020 at 3:29 pm

    Very good article. I will be facing a few of these issues as well.. Onida Conway Jessika

  14. bahis December 18, 2020 at 5:40 pm

    I think the admin of this web site is truly working hard in favor of his site, as here every material is quality based stuff. Bette Far Bertle

  15. sikis December 19, 2020 at 5:08 am

    Appreciation to my father who told me concerning this weblog, this blog is in fact awesome. Ursulina Raphael Temple

  16. erotik December 19, 2020 at 8:43 am

    Hello. This post was really remarkable, particularly since I was looking for thoughts on this issue last Wednesday. Charmaine Edvard Frasier

  17. erotik December 19, 2020 at 10:08 am

    Currently it sounds like Movable Type is the preferred blogging platform available right now. Blake Lind Nevada

  18. bahis December 19, 2020 at 12:08 pm

    Below you will locate the link to some sites that we consider you must visit. La Verne Cori Teevens

  19. sikis December 19, 2020 at 1:25 pm

    Way cool! Some extremely valid points! I appreciate you writing this write-up and also the rest of the site is also very good. Georgeanna Pooh Eckmann

  20. bahis December 19, 2020 at 3:18 pm

    I have recently started a website, the info you offer on this site has helped me greatly. Thanks for all of your time & work. Adriane Hyatt Farley

  21. bluray December 20, 2020 at 6:36 am

    I am genuinely grateful to the owner of this web site who has shared this enormous post at at this place. Agneta Harland Sirmons

  22. yify December 20, 2020 at 8:49 am

    Very interesting details you have noted, regards for putting up. Kristen Ray Ryann

  23. bedava December 20, 2020 at 9:57 am

    Having read this I believed it was really informative. Ursala Martino MacRae

  24. download December 20, 2020 at 2:22 pm

    At this time I am going to do my breakfast, once having my breakfast coming again to read other news. Jaimie Nahum Beltran

  25. yify December 20, 2020 at 3:55 pm

    I like it when folks get together and share views. Austine Jere Iorgo

  26. download December 20, 2020 at 7:21 pm

    Thanks so much for the article. Really thank you! Keep writing. Tiffani Ingmar Taylor

  27. torrent December 20, 2020 at 9:05 pm

    Yes! Finally something about MBBS Admission in Bangladesh. Dani Jamie Biddick

  28. yify December 20, 2020 at 10:31 pm

    Just arrived to your blog from Google. Thanks for this information and analysis that really touched me. Leola Dal Leeanne

  29. yify December 21, 2020 at 5:22 am

    Great post! We will be linking to this particularly great article on our website. Keep up the good writing. Fayre Jefferey Adriene

  30. bedava December 22, 2020 at 11:49 am

    This design is wicked! You obviously know how to keep a reader amused. Maisie Alaster Volotta

  31. torrent December 23, 2020 at 3:57 am

    I consider something truly special in this web site. Nanete Erhart Parent

  32. เกมส์ไพ่ January 1, 2021 at 11:54 pm

    Hey there, You have done a fantastic job. I will definitely digg it and personally suggest
    to my friends. I am sure they’ll be benefited from this site.

  33. StaciTHalt January 3, 2021 at 10:10 pm

    Magnificent site. Plenty of useful information here.
    I am sending it to a few pals ans also sharing in delicious.
    And naturally, thanks on your own sweat!

    Stop by my blog: StaciTHalt

  34. 사설토토 January 15, 2021 at 2:14 pm

    Simply wanna input on few general things, The website style is perfect, the articles is rattling excellent : D.

  35. brown276.kizmasaj.com/2021/01/-sports/ January 16, 2021 at 12:50 am

    Pretty section of content. I just stumbled upon your
    weblog and in accession capital to assert that I acquire in fact
    enjoyed account your blog posts. Anyway I’ll be subscribing to your feeds and even I achievement you access consistently fast.

  36. cook778.memes4ever.com/2.html January 16, 2021 at 5:55 am

    I have been browsing online more than 3 hours these days, but I never
    discovered any interesting article like yours. It’s lovely value sufficient for
    me. Personally, if all site owners and bloggers made
    excellent content as you did, the web can be a
    lot more useful than ever before.

  37. prodigmarketing.com January 16, 2021 at 1:07 pm

    Hmm it looks like your blog ate my first comment (it was extremely long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying your
    blog. I too am an aspiring blog writer but I’m still new to everything.
    Do you have any suggestions for beginner blog writers?
    I’d certainly appreciate it.

  38. dekormio.com January 17, 2021 at 7:09 pm

    Hi! Someone in my Myspace group shared this site with us so I came to give it
    a look. I’m definitely loving the information. I’m bookmarking and will
    be tweeting this to my followers! Superb blog and brilliant style and design.

  39. 한국야동 January 17, 2021 at 7:14 pm

    At this time it seems like BlogEngine is the best blogging platform out there right now.
    (from what I’ve read) Is that what you are using on your blog?

  40. eltangerino.com January 17, 2021 at 9:37 pm

    This website certainly has all the information I needed concerning this
    subject and didn’t know who to ask.

  41. urjajewels.com January 18, 2021 at 12:36 am

    Hi, I log on to your new stuff like every week. Your story-telling style is awesome, keep it up!

  42. Laverne January 18, 2021 at 2:20 am

    What’s up, just wanted to mention, I enjoyed this post.
    It was practical. Keep on posting!

  43. apolloimpact.com January 18, 2021 at 6:13 pm

    Wonderful beat ! I wish to apprentice while you amend your site, how
    could i subscribe for a blog website? The account helped
    me a acceptable deal. I had been a little bit acquainted of this your broadcast
    provided vivid clear idea

  44. tech-fountain.com January 19, 2021 at 6:34 pm

    We are a group of volunteers and opening a new scheme in our community.
    Your site provided us with valuable information to work on. You have done a formidable job and our entire community will
    be thankful to you.

  45. crackpatchs.com January 19, 2021 at 9:47 pm

    When someone writes an piece of writing he/she retains the plan of a user in his/her mind that how a user can be aware of it.
    Therefore that’s why this paragraph is outstdanding.

    Thanks!

  46. watson917.taqnie.com/2021/01/---slot-machines/ January 20, 2021 at 12:07 am

    When someone writes an article he/she keeps the thought of a user in his/her brain that how a user can know it.
    So that’s why this piece of writing is great. Thanks!

  47. Laurence January 20, 2021 at 5:48 am

    Pretty! This has been a really wonderful post. Thank you for providing this information.

  48. makehotfriendship.com January 20, 2021 at 1:53 pm

    Hurrah! Finally I got a website from where I be able to truly obtain valuable information regarding my study and knowledge.

  49. adams126.followerbama.com/2021/01/13/blackjack-8----/ January 20, 2021 at 4:13 pm

    Good day! I could have sworn I’ve visited this blog before
    but after looking at a few of the articles I realized it’s
    new to me. Nonetheless, I’m certainly happy I stumbled
    upon it and I’ll be bookmarking it and checking back frequently!

Leave a comment

Your email address will not be published. Required fields are marked *