xdevel2000
xdevel2000

Reputation: 21364

JavaScript and String as primitive value

In JavaScript a String is a primitive value. But is also a String object... A primitive value is a value put directly into a variable.

So my question is:

var d = "foo";

does d contain directly foo or a reference to a string object like other languages?

Thanks.

Upvotes: 5

Views: 3618

Answers (6)

belykh
belykh

Reputation: 1290

if you define

var d = "foo";

than d contains directly foo but, if you define

var S = new String("foo");

then S is an Object

Example:

var s1 = "1";
var s2 = "1";
s1 == s2 -> true
var S1 = new String("2");
var S2 = new String("2");
S1 == S2 -> false

Upvotes: 4

FarligOpptreden
FarligOpptreden

Reputation: 5043

I found two useful articles detailing this, located here and here. Seems like primitive types in JavaScript are passed by VALUE (i.e. when you pass if to a function it gets "sandboxed" within the function and the original variable's value won't change), while reference types are passed, you guessed it, by REFERENCE and passing it through to a function will change the original variable.

Primitive types in JavaScript are text (string), numeric (float / int), boolean and NULL (and the dreaded "undefined" type). Any custom objects, functions or standard arrays are considered reference types. I haven't researched the Date type though, but I'm sure it will fall into the primitive types.

Upvotes: 1

James Allardice
James Allardice

Reputation: 165971

If I understand it correctly, d will contain the string literal "foo", and not a reference to an object. However, the JavaScript engine will effectively cast the literal to an instance of String when necessary, which is why you can call methods of String.prototype on string literals:

"some string".toUpperCase(); //Method of String.prototype

The following snippet from MDN may help to explain it further (emphasis added):

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives and String objects, so that it's possible to use String object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.

This is all explained in detail in the specification, but it's not exactly easy reading. I asked a related question recently (about why it is possible to do the above), so it might be worth reading the (very) detailed answer.

Upvotes: 7

Matt Fellows
Matt Fellows

Reputation: 6522

I believe there are no primitives in Javascript, in the Java sense at least - everything is an object of some kind. So yes it is a reference to an object - if you extend the String object, d would have that extension.

If you mean primitives as in those types provided by the language, you've got a few, boolean, numbers, strings and dates are all defined by the language.

Upvotes: 0

Mark Cameron
Mark Cameron

Reputation: 2369

Found this page about javascript variables, seems that:

Primitive type for javascript are booleans, numbers and text.

Upvotes: 0

Usch
Usch

Reputation: 80

I think that every variable in Javascript actually represents an Object. Even a function is an Object.

Upvotes: 2

Related Questions