create account

Properties in JavaScript objects By albro by albro

View this thread on: hive.blogpeakd.comecency.com
· @albro ·
$2.46
Properties in JavaScript objects By albro
<center>![Properties in JavaScript objects.png](https://files.peakd.com/file/peakd-hive/albro/23viRECGT3ifLBpad4cckBgGe5xBH2KZuvTAv853HWwpqxGuWVTgPXw4Z984rXmUc41xy.png)</center>

<p>In this post, I want to talk about properties in JavaScript objects. In many ways, it can be said that properties are the most important part of an object, so I will start with them.</p>
<h3>Properties in JavaScript objects</h3>
<p>Properties are values given to an object, and an object can be said to be an unordered set of properties. You must know that we can remove or add or change properties, but some of them are unchangeable. Let's take a look at these.</p>
<h3>Access properties</h3>
<p>How to access the properties of an object in JavaScript is as follows:</p>
<pre><code class="language-javascript">objectName.property         // hive.age</code></pre>
<p>Or</p>
<pre><code class="language-javascript">objectName["property"]      // hive["age"]</code></pre>
<p>Or</p>
<pre><code class="language-javascript">objectName[expression]      // x = "age"; hive[x]</code></pre>
<p>Consider the following two examples.</p>
<p>First example:</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;JavaScript Object Properties&lt;/h2&gt;
&lt;p&gt;There are two different ways to access an object property:&lt;/p&gt;
&lt;p&gt;You can use .property or ["property"].&lt;/p&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var hive = {
  firstname:"Hive",
  lastname:"Blockchain",
  age:3,
  eyecolor:"red"
};
document.getElementById("demo").innerHTML = hive.firstname + " is " + hive.age + " years old.";
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Access properties](https://files.peakd.com/file/peakd-hive/albro/23u6YmoQbBzwUH7qXs2aa15foxoTTzKZ26JynPM89TdF3g9mHqTq96TG1dSWNG8dYqv8i.png)</center>

<p>Second example:</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;JavaScript Object Properties&lt;/h2&gt;
&lt;p&gt;You can use .property or ["property"].&lt;/p&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var hive = {
  firstname:"Hive",
  lastname:"Blockchain",
  age:3,
  eyecolor:"red"
};
document.getElementById("demo").innerHTML = hive["firstname"] + " is " + hive["age"] + " years old.";
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Access properties](https://files.peakd.com/file/peakd-hive/albro/23u6YmnM2guXFFQDe5cu44A1kX2wtu3hQqRbMSXdrhFYp6bKsU22f6fFB3mbBx8bJRS7s.png)</center>

<p>As you can see, both methods return the same result and the choice is yours which method to use.</p>
<h3>Using the <code>for...in</code> loop</h3>
<p>The <code>for...in</code> loop in JavaScript cycles through all the properties of an object and performs a specific action based on the code you wrote for it.</p>
<pre><code class="language-javascript">for (variable in object) {
  // code to be executed
}</code></pre>
<p>The code block placed inside the <code>for...in</code> loop is executed on each object's properties. Consider the following example:</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;JavaScript Object Properties&lt;/h2&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var txt = "";
var platform = {fname:"Hive", lname:"Blockchain", age:3}; 
var x;
for (x in platform) {
  txt += platform[x] + " ";
}
document.getElementById("demo").innerHTML = txt;
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<Center>![Access properties](https://files.peakd.com/file/peakd-hive/albro/23uFwMHi4mrFM2WzpnvwSHiQGcjopdqwgUEJhQobZtYmGgaAR4jLKq6wKCNoq5SA3cKYk.png)</center>

<h3>Add new properties</h3>
<p>To add new properties, just use the same property access rule. Example:</p>
<pre><code class="language-javascript">var platform = {
  firstname:"Hive",
  lastname:"Blockchain",
  age:3,
  eyecolor:"red"
};
platform.nationality = "English";
document.getElementById("demo").innerHTML =
platform.firstname + " is " + platform.nationality + ".";
&lt;/script&gt;</code></pre>
<p>The output of this code will be "Hive is English.".</p>
<p><strong>Note:</strong> You are not allowed to use reserved words to name properties of objects. The rules for naming variables are also true in the field of object properties. Reserved words in programming languages are special words that have been reserved by their developers for special purposes. For example, the word <code>function</code> is a reserved word that is used to define functions. You cannot name your variables <code>function</code>. Of course, you can add something to it; For example, <code>myFucntion</code> is fully accepted.</p>
<h3>Remove properties</h3>
<p>The <code>delete</code> keyword can remove a specific attribute from an object. Consider the following example:</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;JavaScript Object Properties&lt;/h2&gt;
&lt;p&gt;You can delete object properties.&lt;/p&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var platform = {
  firstname:"Hive",
  lastname:"Blockchain",
  age:3,
  eyecolor:"red"
};
delete platform.age;
document.getElementById("demo").innerHTML =
platform.firstname + " is " + platform.age + " years old.";
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Remove properties](https://files.peakd.com/file/peakd-hive/albro/23uRL6XW4GFKtueJWfDzkW6ynEU5CwL8eks1JmLNdUTeoqyU5BqszGciYj2FmT3K5shc9.png)</center>

<p>As you can see, the output of this code is "<code>Hive is undefined years old.</code>", which means that Hive's <code>age</code> has been removed from the object and now the value of <code>undefined</code> has been returned to us.</p>
<p>A few tips about the <code>delete</code> keyword:</p>
<ul>
    <li>
        This keyword removes both the attribute and the value of that attribute.
    </li>
    <li>
        After using <code>delete</code>, you can no longer access that feature unless you create it again.
    </li>
    <li>
        This keyword is made to use properties of objects and has no effect on functions or variables.
    </li>
    <li>
        You should not use this keyword on default properties defined by JavaScript itself. If you do so, your program may stop altogether.
    </li>
</ul>
<h3>What is Attribute?</h3>
<p>All properties have a name and a value. Now this name is considered an attribute for that property. Other attributes include <code>enumerable</code>, <code>configurable</code>, and <code>writable</code>. In fact, the job of these attributes is to determine how a feature can be accessed. For example, is it readable? Is it also writable?</p>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 238 others
properties (23)
authoralbro
permlinkproperties-in-javascript-objects-by-albro
categoryhive-169321
json_metadata"{"app":"peakd/2023.11.3","format":"markdown","author":"albro","description":"In this post, I want to talk about properties in JavaScript objects.","tags":["development","programming","gosh","threads","tricks","neoxian","hive-engine","lassecash","chessbrothers","leofinance"],"users":[],"image":["https://files.peakd.com/file/peakd-hive/albro/23viRECGT3ifLBpad4cckBgGe5xBH2KZuvTAv853HWwpqxGuWVTgPXw4Z984rXmUc41xy.png","https://files.peakd.com/file/peakd-hive/albro/23u6YmoQbBzwUH7qXs2aa15foxoTTzKZ26JynPM89TdF3g9mHqTq96TG1dSWNG8dYqv8i.png","https://files.peakd.com/file/peakd-hive/albro/23u6YmnM2guXFFQDe5cu44A1kX2wtu3hQqRbMSXdrhFYp6bKsU22f6fFB3mbBx8bJRS7s.png","https://files.peakd.com/file/peakd-hive/albro/23uFwMHi4mrFM2WzpnvwSHiQGcjopdqwgUEJhQobZtYmGgaAR4jLKq6wKCNoq5SA3cKYk.png","https://files.peakd.com/file/peakd-hive/albro/23uRL6XW4GFKtueJWfDzkW6ynEU5CwL8eks1JmLNdUTeoqyU5BqszGciYj2FmT3K5shc9.png"]}"
created2023-12-03 21:30:00
last_update2023-12-03 21:30:00
depth0
children3
last_payout2023-12-10 21:30:00
cashout_time1969-12-31 23:59:59
total_payout_value1.240 HBD
curator_payout_value1.217 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,652
author_reputation30,477,419,385,789
root_title"Properties in JavaScript objects By albro"
beneficiaries
0.
accounthive-169321
weight200
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,383,821
net_rshares5,078,506,344,084
author_curate_reward""
vote details (302)
@chessbrotherspro ·
<h3>Congratulations!</h3><hr /><div class="pull-right"><img src="https://files.peakd.com/file/peakd-hive/chessbrotherspro/AJoJKGVARKHFCTHG7ee3GNkn5RMN7wixeJ52ipAgzDZ4QmeTcBdsk8hpi4pgj4e.png" alt="You have obtained a vote from CHESS BROTHERS PROJECT"/></div><div class="text-justify"><h3>✅ Good job. Your post has been appreciated and has received support from <a href="/@chessbrotherspro"><b>CHESS BROTHERS</b></a> ♔ 💪</h3><p><br>♟ We invite you to use our hashtag <b>#chessbrothers</b> and learn more <a href="/@chessbrotherspro/introducing-chess-brothers-project-the-most-innovative-community-combining-chess-fitness-and-more"><b>about us</b></a>.</p><p>♟♟ You can also reach us on our <a href="https://discord.gg/73sK9ZTGqJ" rel="noopener" title="This is going to take you to the Discord of Chess Brothers"><b>Discord server</b></a>  and promote your posts there. </p><p>♟♟♟  Consider <a href="/@chessbrotherspro/teamwork-is-worthwhile-join-the-chess-brothers-healing-trail-supporting-the-work-being-done-and-earning-rewards"><b>joining our curation trail</b></a> so we work as a team and you get rewards automatically.</p><p>♞♟ Check out our <a href="/@chessbrotherspro"><b>@chessbrotherspro</b></a> account to learn about the curation process carried out daily by our team.</p><br>🥇 If you want to earn profits with your HP delegation and support our project, we invite you to join the <i>Master Investor</i> plan. <a href="/@chessbrotherspro/master-investor-plan-or-programa">Here you can learn how to do it.</a></div><div class="text-center"><p><br>Kindly</p><p><strong><em>The CHESS BROTHERS team</em></strong></p></div>
properties (22)
authorchessbrotherspro
permlinkre-properties-in-javascript-objects-by-albro-20231205t165829z
categoryhive-169321
json_metadata"{"app": "beem/0.24.26"}"
created2023-12-05 16:58:30
last_update2023-12-05 16:58:30
depth1
children0
last_payout2023-12-12 16:58:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,628
author_reputation77,931,018,286,388
root_title"Properties in JavaScript objects By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,435,768
net_rshares0
@poshthreads ·
https://inleo.io/threads/albro/re-leothreads-czvypwew
<sub> The rewards earned on this comment will go directly to the people ( albro ) sharing the post on LeoThreads,LikeTu,dBuzz.</sub>
properties (22)
authorposhthreads
permlinkre-albro-properties-in-javascript-objects-by-albro-1123
categoryhive-169321
json_metadata"{"app":"Poshtoken 0.0.2","payoutToUser":["albro"]}"
created2023-12-04 06:46:36
last_update2023-12-04 06:46:36
depth1
children0
last_payout2023-12-11 06:46:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length187
author_reputation415,471,585,053,248
root_title"Properties in JavaScript objects By albro"
beneficiaries
0.
accountnomnomnomnom
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id129,395,020
net_rshares0
@stemsocial ·
re-albro-properties-in-javascript-objects-by-albro-20231208t041019989z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-albro-properties-in-javascript-objects-by-albro-20231208t041019989z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2023-12-08 04:10:18
last_update2023-12-08 04:10:18
depth1
children0
last_payout2023-12-15 04:10:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,915,413,852,146
root_title"Properties in JavaScript objects By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,499,704
net_rshares0