New course! Every coder should learn Generative AI!
Try a free lesson+ 5
Why do I get undefined as the output of my code after I've clearly assigned a value to x?
7 Answers
+ 3
Sweet lord. That's not what I'm talking about. x is a string, right? String is a primitive datatype, so it cannot have properties (such as y). It doesn't matter what datatype would y be, strings still cannot have properties
+ 4
Because you are trying to access a property of an object that isn't in object format. Try something like this:
Var x = {};
X.norm = "hello";
X.y = 8865;
+ 3
It's because primitive datatypes cannot have properties. Adding a property to it doesn't magically make it an object, because then what do you do with the initial value?
+ 3
Airree I'm sorry but I really don't understand what you mean. Maybe you can still help me by explaining to me exactly what you mean in a much simpler text with less terminologies 🤦♂️
+ 3
String: primitive (simple)
Object: not primitive (not simple)
Object: has properties
String: doesn't have properties
Can't add properties to strings, because it's primitive (not object)
+ 3
Airree even though I assign the value of x, y and x.y to be a string, I still get undefined as the output
+ 3
Oh, I get you now Airree