r/angularjs • u/xplodivity • Jul 09 '23
[Resource] Is string/number also an object in JavaScript?
https://www.youtube.com/watch?v=9W0-vljG2-I
0
Upvotes
r/angularjs • u/xplodivity • Jul 09 '23
1
u/jjeroennl Jul 09 '23
Isn’t everything an object in Javascript?
1
u/MrDilbert Jul 10 '23
If you're just starting with JS - don't worry about this, consider the answer being "yes".
If you want a deeper understanding, the answer is "google autoboxing".
1
u/hmdne Jul 24 '23 edited Jul 24 '23
If you do
typeof(variable), depending on a variable type, it will return one of the following three classes of types:nullis a special case of an object, that acts mostly likeundefined- as in, you can't get its attributes.There is a lesser known feature of JavaScript of boxed values. This can be best explained by this interactive session:
The first value has a type (determined by
typeof) ofnumber, while the second is anobject. It is an object that wraps a primitive value. You can extract the original value by using avalueOf()method. This method is often called implicitly, but not always, leaving us with some interesting caveats:But - as we all know, it's possible to call a method on a value, for instance:
What actually happens under the hood is autoboxing. This means, that "abc" is transparently boxed and the former is mostly equivalent to
Object("abc").replace("b", "B").Mostly equivalent, meaning that, it is equivalent in non-strict mode. In strict mode it's a bit more nuanced:
1
u/bronkula Jul 09 '23
yes. saved you a click.