Skip to content

Instantly share code, notes, and snippets.

@kj786
Last active August 29, 2015 14:17
Show Gist options
  • Save kj786/f7ef2163738d2a37ea86 to your computer and use it in GitHub Desktop.
Save kj786/f7ef2163738d2a37ea86 to your computer and use it in GitHub Desktop.
What is practical difference between String(...) and new String(...)
I failed to properly understand the way String(..) works in following portion:
return new TextCell(String(row[name]));
from http://eloquentjavascript.net/06_object.html
The answer is below https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String:
Distinction between string primitives and String objects
Note that JavaScript distinguishes between String objects and primitive string values. (The same is true of Boolean and Numbers.)
String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. 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.
http://stackoverflow.com/questions/3945202/whats-the-difference-between-stringvalue-vs-value-tostring
http://www.adequatelygood.com/Object-to-Primitive-Conversions-in-JavaScript.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment