4

when I am doing the following snippet

var name = new String("NewDelhi");
var count = new Number(10);
console.log(name instanceof String); //false
console.log(count instanceof Number); //true

when I am using name as variable it showing me false while giving it some other variable it showing true

var str = new String("NewDelhi");
var count = new Number(10);
console.log(str instanceof String); //true
console.log(count instanceof Number); //true

why is this happening.

2
  • 8
    This would be the issue in hiding window variables (window.name, which is a primitive, so window.name instanceof String === false) with silly variable names Commented May 27, 2015 at 11:41
  • ^ that, you're accessing window.name, which can't be an object Commented May 27, 2015 at 11:45

4 Answers 4

1

That's because name is not a variable, it's a property in the window object. When you try to create a global variable named name, that will be ignored and the existing property will be used instead.

The type of the property is a string primitive, not a string object. The type of a variable is dynamic, so it could both hold a string primitive or a String object, but the property has a specific type and can only hold a string primitive.

typeof name will return "string", not "object". As it's not an object, it's not an instance of the String class.

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

Comments

0

I just wrote and runned a little snippet

function test(){
    window.name = 'Hello World';
    alert(name);
}

If you try it you will see it will output "Hello World", so your problem is caused by the scope of the variable.
When you are calling console.log(name instanceof String), you are actually doing console.log(window.name instanceof String);


Moreover window.name is a primitive string and not an object, so to check if it is a string try something like this:

alert(typeof name === "string" );

And it will output 'true'!

Comments

0

The previous answers don't seem to emphasize that this is a special case. This behaviour is not only because the global window object already has a name param, but because window.name (=equivalent to the global name in a browser environment) is treated as a special case, ie. always casted into a primitive string.

Other properties defined on the global window object don't have this behaviour, you can try adding a property yourself:

window.xyz = "primitive";
window.xyz = new String("NewDelhi");
window.xyz;
> String {0: "N", 1: "e", 2: "w", 3: "D", 4: "e", 5: "l", 6: "h", 7: "i", length: 8, [[PrimitiveValue]]: "NewDelhi"}

The reason it's treated as such is so that the original semantics of the property can be kept: it is the name of the window, anything but a primitive string wouldn't make sense.

See this discussion for more details (which might even warrant marking this question as a duplicate).

3 Comments

It's not really a special case. There are lots of properties which exhibit this behaviour
@RGraham my issue was mostly that if someone isn't familiar with the language, from the other answers might have concluded that a property's type is somehow always static - which is not the general case.
I see what you mean - that there are properties on window which are special cases, rather than window.name specifically
0

"name" is kind of a reserved property of the Window object.

Even though you initialize it as global variable it is ignored by default as it is already the property of Window object.

var name = new String("NewDelhi");

Now name is the property of the Window object and not the instance of String class.

The property can only hold string primitive.

Thank you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.