+ 2
Why does Object.keys("a string"); returns array of string characters
Since a string is primitive data type in JavaScript then why such behaviour. Ideally Object.keys() should work with Objects only.
7 Answers
+ 1
Hello vejou namaste bhay are you okay
0
"JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup." from MDN WebDocs
edit:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
0
So why we can't store properties in string?