-3

Difference between the javascript String Type and String Object? does not include case 2.


All of these expressions seem to do mostly the same thing. How are they different?

  1. 'message'

  2. String('message');

  3. new String('message');

6
  • new String('message') !== 'message' is the big one. Commented Aug 2, 2014 at 5:51
  • See stackoverflow.com/questions/2051833/… Commented Aug 2, 2014 at 5:53
  • 1
    Never do 2 and 3. Never. (Well maybe in some cases you would use it but normally you don't and you shouldn't) If you use it and you don't know what you're doing you will get mysterious errors in your code. Commented Aug 2, 2014 at 5:57
  • In what situations do 2 and 3 break things but 1 does not? Commented Aug 2, 2014 at 6:05
  • @user2357112 Are there situations in which 2 and 3 behave differently? Commented Aug 2, 2014 at 6:09

1 Answer 1

-1

Basically 'message' is just a string litrel. You can just use String functions on that. What I know that String('message') & 'message' are same.

But on new String() create a new Object of type String. So you will be able to define your properties & methods on it.

Like bellow

var msg = new String('message');
msg.sender = "Someone".
msg.sender //returns "Someone"

var msg = String('message');
msg.sender = "Someone".
msg.sender //returns undefined

var msg = 'message';
msg.sender = "Someone".
msg.sender //returns undefined

But you can't do sane on litrels.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the difference between String('message') and new String('message')?
@jnylen What I know that String('message') & 'message` are same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.