Ramu26 Posted November 20, 2014 Share Posted November 20, 2014 Hi, var str="something" typeof(str) -----> "String" var strObject = new String() typeof(strObject) -----> Object I was expecting typeof(strObject) to return String, Yes it is a object, but, of type String. Does that mean "String" dataType is primitive and not a object Link to comment Share on other sites More sharing options...
justsomeguy Posted November 20, 2014 Share Posted November 20, 2014 Javascript is a little bit weird like that with types since essentially everything in Javascript is an object. There's some information about it here:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeofNote, for example, that this creates an object: var str = new String('test');But this creates a string: var str = String('test');You should never use that though, it's always better to do this: var str = 'test';There are other objects for the other types as well, e.g.: var a = String('test');var b = Boolean(true);var c = Number(1);var d = Array();var e = [];console.log(typeof a); // "string"console.log(typeof ; // "boolean"console.log(typeof c); // "number"console.log(typeof d); // "object"console.log(typeof e); // "object"console.log(typeof undefined); // "undefined"console.log(typeof null); // "object" 1 Link to comment Share on other sites More sharing options...
davej Posted November 20, 2014 Share Posted November 20, 2014 Javascript seems to have a number of constructors that are best avoided unless you really understand their unexpected quirks. Most Javascript books will discuss this topic. Usually they provide a number of recommendations that avoid the use of the constructors, such as... var a = []; // create a new empty arrayvar b = {}; // create a new empty objectvar c = ''; // create a new empty string Link to comment Share on other sites More sharing options...
Ramu26 Posted November 21, 2014 Author Share Posted November 21, 2014 (edited) One more thing, I was reading Wrox professional Javascript book. It states that there are six DataTypes undefined,null,String,Number,Boolean primitives and Object which is complex. But when did var f= function() {} typeof(f). It gives "function". That means we have function dataType also right? In that case is the book wrong, it is 3rd edition. How many datatypes are really there then ? Edited November 21, 2014 by Ramu26 Link to comment Share on other sites More sharing options...
justsomeguy Posted November 21, 2014 Share Posted November 21, 2014 Check the link I posted, there is information about types there and links to other pages. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now