8

When are Javascript datatypes supposed to be declared as object? They slow down execution speed, and produce nasty side effects, so Why is this possible?

According to W3Schools:http://www.w3schools.com/js/js_numbers.asp enter image description here Also from http://www.w3schools.com/js/js_datatypes.asp enter image description here

Dave
  • 598
  • 2
  • 4
  • 17
  • 5
    What are these nasty side effects of which you speak? – Bucket Jul 07 '14 at 22:04
  • Never heard about any nasty side effects .. care to explain? – Monideep Jul 07 '14 at 22:06
  • 1
    Unsubstantiated claim is unsubstantiated.... Also **everything** in javascript is technically an object... – Ryan Jul 07 '14 at 22:07
  • 1
    What would it even look like to *declare String, Number and Boolean as Objects*? – Pointy Jul 07 '14 at 22:09
  • the more i read this question the less sure i am of what is being asked. are you talking about `var n = new Number(1);` v. `var n = 1;`, or `var n = new Object(1);`, or maybe `Number = Object;`??? – user428517 Jul 07 '14 at 22:11
  • I think he means `var n = new Number(1);` vs `var n = 1;`. Its a legit question. OP must have heard somewhere wrongly about `nasty side effects`. – bits Jul 07 '14 at 22:21
  • in that case he's talking about the difference between `new` and object literals. there's not much functional difference, but you can read more here: http://stackoverflow.com/questions/4597926/creating-objects-new-object-or-object-literal-notation – user428517 Jul 07 '14 at 22:22
  • 1
    var x = 123; var y = new Number(123); (x === y) // is false because x is a number and y is an object. I though this was known but as bits said I think this is a legit question I don't think I should be getting minus points for it. Check here http://www.w3schools.com/js/tryit.asp?filename=tryjs_object_number – Dave Jul 07 '14 at 22:47
  • I am new to Javascript I do not know what nasty effects are W3Schools talking about if someone knows than please feel free. I think W3Schools is reliable. No? – Dave Jul 07 '14 at 22:55

2 Answers2

8

Numbers, strings and booleans can be both primitives and objects. For example you can create a string which is a primitive, and you can create an other which is an object:

var name = 'John Doe';
var email = new String('john@example.com');

The difference is that objects (in this case email) have a lot of useful string manipulation methods. Because of that objects require more memory than primitives. So it's advised to create only primitive values and make the object conversion only when needed. JavaScript does this automatically. For example:

var name = 'John Doe'; // This is a primitive.
var email = 'john@example.com'; // This is an other primitive.

The concatenation of the two is an other primitive:

var to = name + ' <' + email + '>';

However when a method is invoked on the primitive, temporarily email becomes an object:

var index = email.indexOf('@');

Because the conversion to object is happening automatically, you don't need to worry about that. Declare your variables as primitives and JavaScript will convert it to an object when needed.

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
  • you mean when we do var index = email.indexOf('@'); JS will temporarily create an object but then return the index and clear the temp object from the memory? – Dave Jul 07 '14 at 23:05
  • @Dave Yes. When `indexOf` is called, JavaScript temporarily converts the string to an object, but after that `email` will be a primitive again. See here: http://jsfiddle.net/87kD9/ – Gergo Erdosi Jul 07 '14 at 23:12
3

Declaring String, Number and Boolean as object results in:

  1. Slow Performance: creating an object via new keyword is always costly and results in slow execution because it sets a lot of properties and performs underlying activities before returning the object instance. For more detail check below link for details on each operation performed by new keyword. https://zeekat.nl/articles/constructors-considered-mildly-confusing.html

  2. Nasty side effects: when you declare string, number and boolean as object you can face real issues when trying to compare. eg:

    var x = "Hello";
    var y = new String("Hello");
    console.log (x===y) // false because x is String n y is object 
    
    var x = new String("Hello");
    var y = new String("Hello");
    console.log(x==y); // false because objects can't be compared
    
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
shilpa
  • 41
  • 1
  • 1
  • 4