Can someone please explain in plain english what is going on in a particular part of my code for objects?












1















although it is a very simple code, I would like to get a full understanding of what is happening in my condition:



let getFreqOn = function(string){

//set a variable for object

let object = {}

for (let key = 0; key < string.length; key++){

// if (object.hasOwnProperty(string[key])) {

// if (object[string[key]]) {

// if (object[string[key]] !== undefined) {

if (string[key] in object) {

object[string[key]]++
}
else{
object[string[key]] = 1
}
}
return object
}


My main concern would be the first condition, I understand what it is they do but I cant put in to plain English how it is working. For example if (string[key] in object) is basically telling my that if a specific property is in the empty object I defined, then I will set then it will be set as the property and incremented. But what I'm trying to wrap my head around is that the object is empty, so how can the property be in the object?



Hoping someone can enlighten me on the conditions that I commented out as well. Sorry for the noob question.










share|improve this question

























  • Your question is exactly what that if test is about.

    – Pointy
    Nov 20 '18 at 21:11
















1















although it is a very simple code, I would like to get a full understanding of what is happening in my condition:



let getFreqOn = function(string){

//set a variable for object

let object = {}

for (let key = 0; key < string.length; key++){

// if (object.hasOwnProperty(string[key])) {

// if (object[string[key]]) {

// if (object[string[key]] !== undefined) {

if (string[key] in object) {

object[string[key]]++
}
else{
object[string[key]] = 1
}
}
return object
}


My main concern would be the first condition, I understand what it is they do but I cant put in to plain English how it is working. For example if (string[key] in object) is basically telling my that if a specific property is in the empty object I defined, then I will set then it will be set as the property and incremented. But what I'm trying to wrap my head around is that the object is empty, so how can the property be in the object?



Hoping someone can enlighten me on the conditions that I commented out as well. Sorry for the noob question.










share|improve this question

























  • Your question is exactly what that if test is about.

    – Pointy
    Nov 20 '18 at 21:11














1












1








1


0






although it is a very simple code, I would like to get a full understanding of what is happening in my condition:



let getFreqOn = function(string){

//set a variable for object

let object = {}

for (let key = 0; key < string.length; key++){

// if (object.hasOwnProperty(string[key])) {

// if (object[string[key]]) {

// if (object[string[key]] !== undefined) {

if (string[key] in object) {

object[string[key]]++
}
else{
object[string[key]] = 1
}
}
return object
}


My main concern would be the first condition, I understand what it is they do but I cant put in to plain English how it is working. For example if (string[key] in object) is basically telling my that if a specific property is in the empty object I defined, then I will set then it will be set as the property and incremented. But what I'm trying to wrap my head around is that the object is empty, so how can the property be in the object?



Hoping someone can enlighten me on the conditions that I commented out as well. Sorry for the noob question.










share|improve this question
















although it is a very simple code, I would like to get a full understanding of what is happening in my condition:



let getFreqOn = function(string){

//set a variable for object

let object = {}

for (let key = 0; key < string.length; key++){

// if (object.hasOwnProperty(string[key])) {

// if (object[string[key]]) {

// if (object[string[key]] !== undefined) {

if (string[key] in object) {

object[string[key]]++
}
else{
object[string[key]] = 1
}
}
return object
}


My main concern would be the first condition, I understand what it is they do but I cant put in to plain English how it is working. For example if (string[key] in object) is basically telling my that if a specific property is in the empty object I defined, then I will set then it will be set as the property and incremented. But what I'm trying to wrap my head around is that the object is empty, so how can the property be in the object?



Hoping someone can enlighten me on the conditions that I commented out as well. Sorry for the noob question.







javascript object properties key javascript-objects






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 21:10









Pointy

315k44454514




315k44454514










asked Nov 20 '18 at 21:10









TerryTerry

161




161













  • Your question is exactly what that if test is about.

    – Pointy
    Nov 20 '18 at 21:11



















  • Your question is exactly what that if test is about.

    – Pointy
    Nov 20 '18 at 21:11

















Your question is exactly what that if test is about.

– Pointy
Nov 20 '18 at 21:11





Your question is exactly what that if test is about.

– Pointy
Nov 20 '18 at 21:11












1 Answer
1






active

oldest

votes


















0














First, the in operator returns a boolean result. It checks whether the string on the left is present as a property name in the object on the right.



Thus



if (string[key] in object)


asks whether that single character of the string is in use as a property name in the object. As you observed, the very first time through the loop that cannot possibly be true, because the object starts off empty.



Thus the if test is false, so the else part runs. There, the code still refers to object[string[key]], but it's a simple assignment. An assignment to an object property works whether or not the property name is already there; when it isn't, a new object property is implicitly created.



The key difference is right there in the two different statements from the two parts of the if - else:



  object[string[key]]++; // only works when property exists

object[string[key]] = 1; // works always





share|improve this answer
























  • Makes perfect sense. Thanks so much!!!

    – Terry
    Nov 20 '18 at 21:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401580%2fcan-someone-please-explain-in-plain-english-what-is-going-on-in-a-particular-par%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First, the in operator returns a boolean result. It checks whether the string on the left is present as a property name in the object on the right.



Thus



if (string[key] in object)


asks whether that single character of the string is in use as a property name in the object. As you observed, the very first time through the loop that cannot possibly be true, because the object starts off empty.



Thus the if test is false, so the else part runs. There, the code still refers to object[string[key]], but it's a simple assignment. An assignment to an object property works whether or not the property name is already there; when it isn't, a new object property is implicitly created.



The key difference is right there in the two different statements from the two parts of the if - else:



  object[string[key]]++; // only works when property exists

object[string[key]] = 1; // works always





share|improve this answer
























  • Makes perfect sense. Thanks so much!!!

    – Terry
    Nov 20 '18 at 21:35
















0














First, the in operator returns a boolean result. It checks whether the string on the left is present as a property name in the object on the right.



Thus



if (string[key] in object)


asks whether that single character of the string is in use as a property name in the object. As you observed, the very first time through the loop that cannot possibly be true, because the object starts off empty.



Thus the if test is false, so the else part runs. There, the code still refers to object[string[key]], but it's a simple assignment. An assignment to an object property works whether or not the property name is already there; when it isn't, a new object property is implicitly created.



The key difference is right there in the two different statements from the two parts of the if - else:



  object[string[key]]++; // only works when property exists

object[string[key]] = 1; // works always





share|improve this answer
























  • Makes perfect sense. Thanks so much!!!

    – Terry
    Nov 20 '18 at 21:35














0












0








0







First, the in operator returns a boolean result. It checks whether the string on the left is present as a property name in the object on the right.



Thus



if (string[key] in object)


asks whether that single character of the string is in use as a property name in the object. As you observed, the very first time through the loop that cannot possibly be true, because the object starts off empty.



Thus the if test is false, so the else part runs. There, the code still refers to object[string[key]], but it's a simple assignment. An assignment to an object property works whether or not the property name is already there; when it isn't, a new object property is implicitly created.



The key difference is right there in the two different statements from the two parts of the if - else:



  object[string[key]]++; // only works when property exists

object[string[key]] = 1; // works always





share|improve this answer













First, the in operator returns a boolean result. It checks whether the string on the left is present as a property name in the object on the right.



Thus



if (string[key] in object)


asks whether that single character of the string is in use as a property name in the object. As you observed, the very first time through the loop that cannot possibly be true, because the object starts off empty.



Thus the if test is false, so the else part runs. There, the code still refers to object[string[key]], but it's a simple assignment. An assignment to an object property works whether or not the property name is already there; when it isn't, a new object property is implicitly created.



The key difference is right there in the two different statements from the two parts of the if - else:



  object[string[key]]++; // only works when property exists

object[string[key]] = 1; // works always






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 21:15









PointyPointy

315k44454514




315k44454514













  • Makes perfect sense. Thanks so much!!!

    – Terry
    Nov 20 '18 at 21:35



















  • Makes perfect sense. Thanks so much!!!

    – Terry
    Nov 20 '18 at 21:35

















Makes perfect sense. Thanks so much!!!

– Terry
Nov 20 '18 at 21:35





Makes perfect sense. Thanks so much!!!

– Terry
Nov 20 '18 at 21:35


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401580%2fcan-someone-please-explain-in-plain-english-what-is-going-on-in-a-particular-par%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy