Why do I get undefined as the output of my code after I've clearly assigned a value to x? | Sololearn: Learn to code for FREE!
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?

https://code.sololearn.com/WTNBPXAo1rcI/?ref=app

29th Jun 2019, 7:59 PM
eMBee
eMBee - avatar
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
29th Jun 2019, 8:44 PM
Airree
Airree - avatar
+ 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;
30th Jun 2019, 1:00 PM
SapphireBlue
SapphireBlue - avatar
+ 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?
29th Jun 2019, 8:13 PM
Airree
Airree - avatar
+ 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 🤦‍♂️
29th Jun 2019, 8:19 PM
eMBee
eMBee - avatar
+ 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)
29th Jun 2019, 8:22 PM
Airree
Airree - avatar
+ 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
29th Jun 2019, 8:36 PM
eMBee
eMBee - avatar
+ 3
Oh, I get you now Airree
29th Jun 2019, 8:46 PM
eMBee
eMBee - avatar