1

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?

  • Some answers have been offered so far as to Number. This makes it difficult to answer the entire question so I'll just add this as a comment. And that is, with regard to Regexp, using the global instead of /.../ might make it easier to compose a regex built out of variable. – Dexygen Sep 04 '12 at 18:16
  • @GeorgeJempty Nice answer. Too bad it's not an answer. :) – GolezTrol Sep 04 '12 at 18:17
  • For an example of `new Regep(x)` see [this answer](http://stackoverflow.com/a/12240977/36866). You want to use it if you have a string, and I use it there to be able to break it up in lines. It would have been unreadable if it was a literal regexp. – some Sep 04 '12 at 18:20

5 Answers5

2

Having them available allows us to extend them. For example, if the browsers don't implement it, you can add the Array.isArray method yourself.

Array.isArray = function( arr ) {
    return Object.prototype.toString.apply( arr ) === '[object Array]';
};

This is good to have for most polyfills.

Another nice example is the trim method on strings.

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,'');
};

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 Regexp though, as it's convenient to do new 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:

arr.filter( Boolean ); // Removes falsy values from the array "arr"
arr.map( Number ); // Converts every element of the array to a Number
Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
1

When Number() is called as a function, it performs a type conversion on the value passed as an argument. The same is true for String(). String(10) will convert the number 10 to the string '10'

var n1 = 1;
// => 1

var n2 = 's';
// => 's'

But:

var n3 = Number(1);
// => 1

var n4 = Number('s');
// => NaN
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
1

From the MDN :

The primary uses for the Number object are:

If the argument cannot be converted into a number, it returns NaN.

In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.

More generally (I invite you to read the documentations of the other wrapper class in the MDN), those classes are used to perform type conversions or assertions.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

There's a third:

var s1 = new String('string1');

The shorthand var s1 = 'string' is actually a shorthand for the line above. They are pretty much the same.

The other line you gave,

var s1 = String('string1');

, is actually a typecast. You are typecasting a string to a string, which is pointless. But you could also write:

var s1 = String(10);

, in which case you are typecasting the number 10 to a string, which obviously has use in contrast to a string to string typecast.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 1
    There is a *very* important difference between `var s1 = new String('')` and `var s2 = ''`. The value returned by `typeof` will be different. `typeof s1` == `object` `typeof s2`== `string`. – Jeremy J Starcher Sep 04 '12 at 18:22
1

There are several times and places when using an object over a primitive has advantages.

1) Objects can have methods. Something as simple as "hello world".length will "autobox" the primitive string to an object so one can use the length property.

2) Using the String and Number constructor is one method of converting to a string/number, though there are usually better ways.

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74