Skip to content

Instantly share code, notes, and snippets.

@kristyburge
Last active August 1, 2018 20:47
Show Gist options
  • Save kristyburge/2379d5ee5c8e6d511f26fbdd7cf858f2 to your computer and use it in GitHub Desktop.
Save kristyburge/2379d5ee5c8e6d511f26fbdd7cf858f2 to your computer and use it in GitHub Desktop.
Using the new keyword
var str = new String('Hello world');
/*******
You could do the above, but it's best to avoid it (instead do like the variable str2 below)
(because JavaScript knows that anything inside single or double quotes has the type of String)
Same goes for other primitives. This is for example purposes only.
NOTE: To clarify -- the only time I ever use the new keyword in practice is when I use a function constructor and create my own object type.
*******/
var str2 = 'Hello world';
// both have the prototype of String and inherit all the String methods and properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment