Normally, JavaScript strings are primitive values, created from literals: var firstName = “John”

But strings can also be defined as objects with the keyword new: var firstName = new String(“John”)

var x = "John";             // typeof x will return string
var y = new String("John"); // typeof y will return object

Don’t create strings as objects. It slows down execution speed. The new keyword complicates the code. This can produce some unexpected results:

Comparison:

var x = "John";             
var y = new String("John");

// (x == y) is true because x and y have equal values
// (x === y) is false because x and y have different types (string and object)

Even worse:

var x = new String("John");             
var y = new String("John");

// (x == y) is false because x and y are different objects
// (x === y) is false because x and y are different objects

Yonggoo Noh

I am interested in Computer science and Mathematics.