+ 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.

12th Feb 2022, 5:03 PM
Vijay Prakash
Vijay Prakash - avatar
7 Answers
+ 1
Hello vejou namaste bhay are you okay
12th Feb 2022, 9:09 PM
mustapha topo
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
12th Feb 2022, 5:08 PM
Jayakrishna šŸ‡®šŸ‡³
0
So why we can't store properties in string?
13th Feb 2022, 3:30 AM
Vijay Prakash
Vijay Prakash - avatar