javascript: 'String(value)' vs. 'value.toString' What's the difference ?

javascript:   'String(value)'   vs.  'value.toString()'  What's the difference ?

I searched and found a post on 'stackOverflow' that states the only difference is that 'String(value)' is the equivalent of calling 'String("" + value)'  and this causes it to handle null values correctly.

Doing my own testing I found that:
   myStr = String(value);    myType = typeof(myStr);   // 'myType' returns a string value of 'string'
   myLen = myStr.length;     // have to use 'length'  w/o following parentheses or w/parens it throws an error:  TypeError: Cannot call property length in object myValue It is not a function, it is "number"

Open in new window

but, it works if the object string is empty/null ?

Where:
 
 myStr = value.toString();   myType = typeof(myStr);  // 'myType' returns a string value of 'object'
   myLen = myStr.length();    // have to use 'length()' followed w/parentheses or function definition is returned:  "function length() {/* int length()  */}"

Open in new window

But throws an error if the object is empty/null: TypeError: Cannot call property length in object . It is not a function, it is "number".

Is there a foolproof way to get the length w/o testing for variable type and having multiple scenarios ?
Thanks
sidwelleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paul JacksonSoftware EngineerCommented:
Basically the first is returning a primitive string and the second is a String object.

Primitive types don't have methods hence why you can't call .length() which is a method call on an string object.

I'm not completely sure what you are asking for when you ask for a foolproof method, if you create your strings using String(value) then you need to use .length to get the length of the string, if you use value.ToString() then you have to use .length().
It is up to you how the string is created in the code, is it not?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sidwelleAuthor Commented:
This is what I need to know.  I get a value returned by an object that I don't always have control of, so I was looking for one method that would return the length no matter what's the type.

Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.