Tech Junkie Blog - Real World Tutorials, Happy Coding!: JavaScript Objects Deep Dive : Setting And Getting Properties

Monday, October 25, 2021

JavaScript Objects Deep Dive : Setting And Getting Properties

Now that we've learned to create objects in all sorts of ways it's time to set and get the object properties that we've created.  In this blog we will go over how to access the object properties either to set them or get them.

First let's create an object that we can work with:

Type in the following code in the between the <script type="text/javascript"></script> tag in an html page.


        var product = new Object();

        product.name = "Chai";
        product.category = "Tea";
        product.country = "India";
        product.supplier = {
            name: "ACME Tea Of India",
            location: "New Delhi"
        };

        console.log(product);
If you run the page with the code above you will see the following in the browser console:















Now that we have our object let's start changing the properties. There are two ways you can get and set properties. You can either set the properties using the .dot notations or you can use the [] operators to access the properties. First let's get the properties with these two methods. Add the following lines to the code above.

        console.log(product.name);
        console.log(product["category"]);

As you can see they both got the job done if you look at the console in your browser. With both methods we were able to retrieve the values for the product name, and category.









Now let's do the same to set the properties for our product. First let's set the name property to "Dark Roast" using the dot notation. product.name = "Dark Roast"; Now let's set the category to be "Coffee" with the bracket operators. product["category"] = "Coffee"; If we look at the browser console again we will see that the name, and category has been changed.








Here is the complete code for this example:

<!DOCTYPE html>
<html>
<head>
    <title></title>
 <meta charset="utf-8" />
    <script type="text/javascript">
        var product = new Object();

        product.name = "Chai";
        product.category = "Tea";
        product.country = "India";
        product.supplier = {
            name: "ACME Tea Of India",
            location: "New Delhi"
        };

        product.name = "Dark Roast";
        product["category"] = "Coffee";

        console.log(product);
        console.log(product.name);
        console.log(product["category"]);
    </script>
</head>
<body>

</body>
</html>




21 comments:

  1. Looking for LCD TV repair Dubai, LED TV repair Dubai? MIRZA IJAZ ELECTRICAL DEVICES repairing Dubai is here to solve you.

    ReplyDelete
  2. Really I like your article it is easy to reading. You are a good blogger pls keep it up. Thank you so much. We offer incomparable Dissertation Writing Services uk

    ReplyDelete
  3. These JavaScript Objects Deep Dive : Setting And Getting Properties, which are so authentic and reflective to having from them but there are also very beneficial and authorized private blog network links available to getting from them in all over the USA so easily.

    ReplyDelete
  4. With the help of Data mining assignment help you can easily get your assignment done by the experts and that too having thorough support for any assistance, if needed. So, never compromise with your assignment rather than asking for Data mining assignment help.

    ReplyDelete
  5. This blog post provides valuable insights into setting and getting object properties, empowering developers to effectively utilize this fundamental aspect of object-oriented programming. And when it comes to reliable Earthwork service texas in USAunderstanding object properties can aid in optimizing project management and enhancing operational efficiency.

    ReplyDelete
  6. You have flexible options to get and set properties in your code. Atleadingit company in UAE, we understand the significance of efficient property manipulation and can assist you in optimizing your code to ensure smooth execution and enhanced performance. Let us help you leverage the power of object properties in your programming endeavors.

    ReplyDelete
  7. Programming success depends on knowing how to set and retrieve object properties. Accessing and modifying object properties enables developers to customise and optimise their code, opening up limitless possibilities and assuring effective data vinyl fencing edmonton in canadamanagement, just like Edmonton homeowners depend on high-quality residential vinyl fencing to beautify their properties.

    ReplyDelete
  8. Understanding how to access and manipulate object properties is a fundamental skill in programming. This blog provides valuable insights on setting and retrieving object properties, helping developers enhance their coding skills. And for a comprehensive car cleaning experience, consider Ottawa Car Wash for professional and reliable car washing services.

    ReplyDelete
  9. Mastering the art of accessing and manipulating object properties is essential in web development. When it comes to optimizing your online presence, seo services dubaican help enhance visibility, improve search rankings, and drive organic traffic to your website, ensuring your digital success.

    ReplyDelete
  10. The convenience of independently setting and adjusting cooking temperatures for different dishes, resulting in precise cooking and optimal flavor. With its versatile functionality, Best double oven electric rangeempowers you to unleash your culinary creativity and achieve exceptional cooking results.

    ReplyDelete
  11. Just like setting and getting object properties, chemical injection in UAEensures precise control and efficient distribution of chemicals for various industrial applications. Both processes enhance functionality and optimize performance.

    ReplyDelete
  12. "JavaScript Objects Deep Dive: Setting and Getting Properties" is a comprehensive guide that delves into the intricacies of working with objects in JavaScript. As you explore this topic, consider how efficient property setting and getting techniques can be as essential to programming as precise methods employed in INFILL DEMOLITION in Albertaensuring optimal results and seamless functionality.

    ReplyDelete
  13. JavaScript Objects Deep Dive" - Dive into the world of JavaScript objects and explore their complexities. When it's time to refuel during heavy equipment operations, rely on Heavy equipment catering in houston texas for nourishing meals and refreshments. Happy learning!

    ReplyDelete
  14. JavaScript Objects Deep Dive: Setting And Getting Properties" delves into programming intricacies. Just as this exploration dissects code elements,Best Barber Shop in Canada offer a detailed approach to grooming, addressing specific needs and preferences for a tailored experience that goes beyond the surface

    ReplyDelete
  15. JavaScript Objects Deep Dive: Setting And Getting Properties is an insightful exploration of programming. Likewise, explore the world of auto detailing with Ottawa Car Detailing services where attention to detail results in a vehicle that looks as good as new. Just as this deep dive enhances coding skills, quality detailing enhances your car's aesthetics and condition.

    ReplyDelete
  16. Exploring JavaScript objects is like fine-tuning your programming toolkit. Just as you seek quality components, when it comes to kdk air curtain suppliers in uae look for reliable suppliers like KDK. Both in coding and cooling, precision matters! 🖥️❄️ #JavaScript #KDKAirCurtains #ProgrammingTips

    ReplyDelete
  17. JavaScript Objects Deep Dive: Setting and Getting Properties is an essential resource for mastering this foundational programming concept. Just as precision is key in coding, finding thecoffee beans dubai is crucial for coffee enthusiasts. Explore the world of coffee beans in Dubai to elevate your coffee experience to the next level. ☕🖥️🌍

    ReplyDelete
  18. JavaScript Objects Deep Dive: Setting and Getting Properties" is an essential resource for developers looking to master this fundamental concept. Just as you dive deep into JavaScript objects, tank cleaning houstonshould ensure they maintain their tanks with effective tank cleaning solutions.

    ReplyDelete
  19. JavaScript Objects Deep Dive" is an invaluable resource for web developers, and much like the precision needed in brewing coffee beans dubaia perfect cup of coffee, this tutorial dives deep into the intricacies of setting and getting properties in JavaScript objects. Just as the right beans are essential for a flavorful brew.

    ReplyDelete

  20. Mastering object creation and property manipulation opens doors to endless possibilities in programming. Similarly, for crafting versatile and functional spaces, consult a leading fit out company in UAE.

    ReplyDelete
  21. Mastering object creation and property manipulation opens endless possibilities in programming! Just like refining code, trust fit out contractors Dubai to meticulously craft spaces tailored to your exact specifications and needs.





    ReplyDelete

Search This Blog