I can create a number by doing
var n1 = 1;
or
var n1 = Number(1);
same with a string
var s1 = 'string1';
or
var s1 = String('string1');
In what cases would one want to use the Global Object as opposed to the simpler way.
Do the JavaScript Global Objects String and Number have any use when programming?
Similarly for
Regexp, Boolean, Function
Is there any purpose for these which can all be instantiated using the appropriate syntax instead?
Having them available allows us to extend them. For example, if the browsers don’t implement it, you can add the
Array.isArraymethod yourself.This is good to have for most polyfills.
Another nice example is the
trimmethod on strings.Otherwise, there isn’t really other uses. Converting to a number is better done with
+, converting to a string is better done with'' +, etc.The only one I regularly use is
Regexpthough, as it’s convenient to donew Regexp(for saving in a variable, because less escaping is needed or because it allows to break up the regex in multiple lines).Edit: I just thought about another use: