why-why-why?!? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why-why-why?!?

All right: In JavaScript: Objects : Introducing Objects: 2nd ▶️in, it says that there are 2 ways to show/list/whatever the name, favorite color, or in this case, age. And that's upsetting to me. In learning how to program/create things in JavaScript and for most people who are also using Solo-Learn, te they're also learning HTML5, CSS, PHP, C++, okay you get my point: syntax and programming structure is tough enough without throwing in: var x = person.age; var y = person['age']; This makes no sense! 😠

13th Jul 2017, 10:01 PM
Michael
Michael - avatar
3 Answers
+ 7
Syntax is important to manipulate data better. It might look confusing at first but then it all makes sense ☺ In your example and generally speaking, the Dot notation and the Bracket notation do the same job: they access object's properties. However, eventhough the Dot notation is faster and clearer to read, in some situations the Bracket notation gives us the opportunity to do stuff that we can't do with Dot notation, for instance: ■ Access properties by name stored in a variable, e.g: var person = { name: "John" }; var x = "name"; var y = person[x]; alert(y);              // outputs John □ Dot notation doesn't work here: var person = { name: "John" }; var x = "name"; var y = person.x;                   alert(y); // outputs undefined ■  Access properties containing special characters like properties named with a '&' character, e.g: var person = { "son & daughter": true }; alert(person["son & daughter"]);    // outputs true □ Dot notation doesn't work here: alert(person.son & daughter);    // undefined ■ When the property names are dynamically determined (when the exact names are not known until runtime). ■ When using a 'for..in' loop to go through all the properties of an object. You can learn more at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
14th Jul 2017, 1:20 AM
Pao
Pao - avatar
+ 2
Paola- Ohhh, I see. Okay, that makes much more sense to me than just throwing out two different ways to call out object properties with hardly any explanation on or about the use of the way in which we might use them. Thank you. Oh! I also wanted to thank you for that link. That Mozilla site was awesome! It is also huge, with tons of great information about not just JavaScript but many other programming and scripting languages. And even that was just a small part of it. Before I knew it, 7pm rolls around with Jeopardy! on TV and my tummy rumbling. Anyway, thank you for your help and advice and for the link. I appreciate it. 😊
15th Jul 2017, 12:51 PM
Michael
Michael - avatar
+ 2
Another straightforward example: Variables can't start with a number... var 1shot = "prove yourself"; // crash and numbers can't be on the lhs* like this: var 1 = "in a lifetime"; // no That's what the assignment below is trying: var testobj={ 1:'hey' } // fine testobj.1="there"; // unexpected number (1="there"?) testobj[1]="hey-- what?"; // works It may help to note JS expecting different main object 'accessors' based on the type of the individual keys (additional accessors can then arguably be for your convenience): obj = { varkey : "obj.varkey", 'stringkey': "obj['stringkey']", 999:"obj[999]" } * lhs: left-hand side of the 'equals' sign
16th Jul 2017, 4:51 AM
Kirk Schafer
Kirk Schafer - avatar