weird output by instanceof in javascript

instanceof javascript
javascript instanceof string
instanceof typescript
javascript instanceof array
instanceof javascript w3schools
javascript instanceof vs typeof
javascript instanceof number
javascript not instanceof

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.

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.

instanceof, The instanceof operator tests whether the prototype property of a JavaScript Demo: Expressions - instanceof expected output: true. 10. ​. Javascript. Love it or hate it it seems to have become the defacto virtual machine of the internet that Java was supposed to be. And it is odd. Like properly odd. This is a short collection of some weird things that I have noticed about it. I keep a little list of these by my desk for my ow

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'!

Javascript: the weird parts -- Charlie Harvey, This is a short collection of some weird things that I have noticed about it. I wonder what the type of null is … js> typeof null "object" … but of Few more bugs that I found out involving simple calculations and stupid outputs:. JavaScript: Understanding the Weird Parts 4.7 (36,936 ratings) Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.

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).

Class checking: "instanceof", The instanceof operator allows to check whether an object belongs It's funny, but the Class constructor itself does not participate in the check! The java instanceof operator is used to test whether the object is an instance of the specified type (class or subclass or interface). The instanceof in java is also known as type comparison operator because it compares the instance with type. It returns either true or false.

"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.

Comparing the JavaScript typeof and instanceof Operators, log outputs true since date is both an instance of the Date constructor and all non​-primitive objects are instances of Object . Conclusion. The  This one is an oddity not just in JavaScript; it’s actually a prevailing problem in computer science, and it affects many languages. The output of this is 0.30000000000000004. This has to do with an issue called machine precision. When JavaScript tries to execute the line above, it converts the values to their binary equivalents.

10 Oddities And Secrets About JavaScript, It is a collection of JavaScript's oddities and well-kept secrets. Despite this, null is not considered an instance of an object. The output of this is 0.30000000000000004. Strange as it might sound, undefined is not actually a reserved word in JavaScript, even though it has a special meaning and is the  Ok, I am done for today. But in no way these are the only weird things with Javascript. No sir, there are hundreds more stupid things. I am just tired of looking for them for today 🙂 I must probably will do a round 2 some time in the future. Excercise for home, can you find out what the following will output and why without running it?

Master the Weird Parts of the JavaScript, That is because a flaw in JavaScript's parser tries to parse the dot notation on a The above outputs bar undefined and foo null - only baz was removed and is When method gets called on an instance of Bar , this will now refer to that very  The difference in associativity comes into play when there are multiple operators of the same precedence. With only one operator or operators of different precedences, associativity does effect the output, as seen in the example above. In the example below, observe how associativity effects the output when multiple of the same operator are used.

The 10 Most Common Mistakes JavaScript Developers Make, If you need help figuring out why your JavaScript isn't working, consult this list of the 10 If you guess that the console.log() call would either output undefined or throw an error, Let's define a simple object, and create and instance of it, as follows: It's funny you mention PHP, because Javascript is the new PHP where it's  The output would be xyz. Here, emp1 object has company as its prototype property. The delete operator doesn't delete prototype property. emp1 object doesn't have company as its own property. You can test it console.log(emp1.hasOwnProperty('company')); //output : false.

Comments
  • 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
  • ^ that, you're accessing window.name, which can't be an object
  • 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