0

Following are some declaration of String in JAVASCRIPT

var str = "Hello World"
var str1 = new String("Hello World")
str2 = "Hello World"

What is the difference between declarations mentioned above. Memory wise or if any.

Adi7
  • 51
  • 6
  • In JS there's absolutely no reason to use `new String()`, it's a weakly typed language and that should be taken advantage of – Ben Kolya Mansley Jan 28 '18 at 16:56
  • See https://stackoverflow.com/questions/10951906/why-does-foo-new-stringfoo-evaluate-to-false-in-javascript – guest271314 Jan 28 '18 at 16:57
  • Possible duplicate of [Why does ("foo" === new String("foo")) evaluate to false in JavaScript?](https://stackoverflow.com/questions/10951906/why-does-foo-new-stringfoo-evaluate-to-false-in-javascript) – banan3'14 Jan 28 '18 at 17:02
  • I read across various sites and books and came to these syntax's. I do not want to find the equality between them. i just want to know what is the difference. – Adi7 Jan 28 '18 at 17:07
  • @Adi7 https://stackoverflow.com/questions/30984463/difference-between-string-primitive-and-string-wrapper-object-in-javascript? – guest271314 Jan 28 '18 at 17:18

1 Answers1

0

I believe the top two are equivalent and the first is just syntactic sugar for the 2nd.

The 3rd assigns the str2 variable to the global window scope. You can test this by going into the Chrome Dev tools and typing that line and inspecting it with the following line: window.str2 will print the value you just assigned.

ballmerspeak
  • 157
  • 2
  • 14