Code like this:
var str = \"Hello StackOverflow !\";
alert(typeof str);
gives me string
as result. This means strings are not
Strings are actually a special kind of objects. You can't add properties to them, but you can modify the String.prototype
, which is the prototype of any string
variable, and add properties to it, like this:
String.prototype.foo = 1;
var a = 'hello';
console.log(a.foo); // logs 1
JavaScript is a prototype base language and you can define properties for your variables. Strings also have their predefined properties and methods to be used.
more information
This depends on how you defines object and what are you referring to when you say string. When you use the word string, you can be referring to just the primitive, or the wrapper object.
In JavaScript there are 5 primitive types: undefined, null, boolean, string and number. Everything else is an object.
Unlike objects, primitives don't really have properties. They exist as values. This explains why you cannot assign a property to a string:
var archy = "hello";
archy.say = "hello";
console.log(archy.say); // undefined
But sometimes manipulating a primitive would make one feel as if she/he is manipulating an object because primitives appear to have methods.
var archy = "hello";
console.log(archy.length); //5
This is due to the fact that JavaScript creates a wrapper object when you attempt to access any property of a primitive.
Here is an extract from Javascript: The Definitive Guide
The temporary objects created when you access a property of a string, number, or boolean are known as wrapper objects, and it may occasionally be necessary to distinguish a string value from a String object or a number or boolean value from a Number or Boolean object. Usually, however, wrapper objects can be considered an implementation detail and you don’t have to think about them. You just need to know that string, number, and boolean values differ from objects in that their properties are read-only and that you can’t define new properties on them.
Strings are pure objects: http://www.ecma-international.org/ecma-262/5.1/#sec-15.5
So the question is, what does the typeof
operator. It simply acts according to its ECMA specification:
http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3
A string like "Hello" is not an object in JavaScript, but when used in an expression like
"Hello".indexOf(2)
A new object derived from the constructor function String
is produced wrapping the string "Hello"
. And indexOf
is a property of String.prototype
so things work as expected, even though there is a lot of magic going on.
In the following case
> var s = "xyz"; s.prop = 1; console.log(s.prop);
undefined
The reason you see undefined
is that:
s
is given a value which is a primitive strings.prop = 1
and property named prop
is assigned to a new, anonymous wrapper object.s
. That is not the same wrapper object as in the second statement, and it does not have a prop
property, so undefined
is produced when asking for its value according to the basic JavaScript rules.