Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:57:45+00:00 2026-05-11T01:57:45+00:00

Just curious: 4 instanceof Number => false new Number(4) instanceof Number => true? Why

  • 0

Just curious:

  • 4 instanceof Number => false
  • new Number(4) instanceof Number => true?

Why is this? Same with strings:

  • 'some string' instanceof String returns false
  • new String('some string') instanceof String => true
  • String('some string') instanceof String also returns false
  • ('some string').toString instanceof String also returns false

For object, array or function types the instanceof operator works as expected. I just don’t know how to understand this.

[new insights]

Object.prototype.is = function() {         var test = arguments.length ? [].slice.call(arguments) : null            ,self = this.constructor;         return test ? !!(test.filter(function(a){return a === self}).length)                : (this.constructor.name ||                   (String(self).match ( /^function\s*([^\s(]+)/im )                     || [0,'ANONYMOUS_CONSTRUCTOR']) [1] ); } // usage var Newclass = function(){};  // anonymous Constructor function var Some = function Some(){}; // named Constructor function (5).is();                     //=> Number 'hello world'.is();           //=> String (new Newclass()).is();        //=> ANONYMOUS_CONSTRUCTOR (new Some()).is();            //=> Some /[a-z]/.is();                 //=> RegExp '5'.is(Number);               //=> false '5'.is(String);               //=> true 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:57:46+00:00Added an answer on May 11, 2026 at 1:57 am

    value instanceof Constructor is the same as Constructor.prototype.isPrototypeOf(value) and both check the [[Prototype]]-chain of value for occurences of a specific object.

    Strings and numbers are primitive values, not objects and therefore don’t have a [[Prototype]], so it’ll only work if you wrap them in regular objects (called ‘boxing’ in Java).

    Also, as you noticed, String(value) and new String(value) do different things: If you call the constructor functions of the built-in types without using the new operator, they try to convert (‘cast’) the argument to the specific type. If you use the new operator, they create a wrapper object.

    new String(value) is roughly equivalent to Object(String(value)), which behaves the same way as new Object(String(value)).


    Some more on types in JavaScript: ECMA-262 defines the following primitive types: Undefined, Null, Boolean, Number, and String. Additionally, there is the type Object for things which have properties.

    For example, functions are of type Object (they just have a special property called [[Call]]), and null is a primitive value of type Null. This means that the result of the typeof operator doesn’t really return the type of a value…

    Aditionally, JavaScript objects have another property called [[Class]]. You can get it via Object.prototype.toString.call(value) (this will return '[objectClassname]'). Arrays and functions are of the type Object, but their classes are Array and Function.

    The test for an object’s class given above works when instanceof fails (e.g. when objects are passed between window/frame boundaries and don’t share the same prototypes).


    Also, you might want to check out this improved version of typeof:

    function typeOf(value) {     var type = typeof value;      switch(type) {         case 'object':         return value === null ? 'null' : Object.prototype.toString.call(value).             match(/^\[object (.*)\]$/)[1]          case 'function':         return 'Function';          default:         return type;     } } 

    For primitives, it will return their type in lower case, for objects, it will return their class in title case.

    Examples:

    • For primitives of type Number (eg 5), it will return 'number', for wrapper objects of class Number (eg new Number(5)), it will return 'Number';

    • For functions, it will return 'Function'.

    If you don’t want to discern between primitive values and wrapper objects (for whatever, probably bad reason), use typeOf(...).toLowerCase().

    Known bugs are some built-in functions in IE, which are considered 'Object' and a return value of 'unknown' when used with some COM+ objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Like in most programming languages, certain keywords are reserved. Javascript… May 11, 2026 at 12:44 pm
  • added an answer This primarily matters when used with composite indexes: CREATE INDEX… May 11, 2026 at 12:44 pm
  • added an answer Install a SIGDIE handler that calls Carp::confess? Or just set… May 11, 2026 at 12:44 pm

Related Questions

Just curious: 4 instanceof Number => false new Number(4) instanceof Number => true? Why
Just curious here: is it possible to invoke a Windows Blue Screen of Death
I'm just curious if any project exists that attempts to group all (or most)
I'm just curious how most people make their ASP.NET pages printer-friendly? Do you create
I'm just curious what Afx stands for. And what about Fx in FxCop?
Questions #1 through #4 on the Joel Test in my opinion are all about
Effective Java (Second Edition) , Item 4, discusses using private constructors to enforce noninstantiability.
What's the Hi/Lo algorithm? I've found this in the NHibernate documentation (it's one method
Let's say you have a small calculator program that takes numbers and an operator

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.