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

  • SEARCH
  • Home
  • 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 3493478
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:53:15+00:00 2026-05-18T11:53:15+00:00

What is the quickest and cleanest way to test if a var holds immutable

  • 0

What is the quickest and cleanest way to test if a var holds immutable data (i.e. is string, number, boolean, null, undefined)?

E.g. When var test is mutable, the following is legal:

var test = {}; 
//the next 2 lines work because `test` has mutable data
test.someFun = function () { alert(this); };
test.someFun(); 

but when var test is immutable, it’s illegal:

var test = "string"; 
//the next 2 lines are invalid, as `test` is a primitive
test.someFun = function () { alert(this); };
test.someFun(); 
  • 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. Editorial Team
    Editorial Team
    2026-05-18T11:53:15+00:00Added an answer on May 18, 2026 at 11:53 am
    var test = {};
    if(test instanceof Object){
      test.someFun = function () { alert(this); };
      test.someFun(); 
    }
    

    jball pointed out the following:

    For the instanceof check to work, the
    left-hand side object should be from
    the same scope as the right-hand side
    object. This is because even the
    common JavaScript objects have
    different copies in different scopes,
    i.e. window1.String !== window2.String.
    (Quote MDN)

    Seems a valid point to me. So if you want to do this check across different scopes you will need to use the workaround:

    var test = {};
    if(test && (typeof test == 'object' || typeof test == 'function')){
      test.someFun = function () { alert(this); };
      test.someFun(); 
    }
    

    I would try to avoid the use of iframes though.
    The null check is needed because typeof(null) is equal to 'object'.

    EDIT:

    box9 and jball did some performance testing on all the methods proposed (+1 for that). I used Chrome, Firefox and Internet Explorer. The result are as expected. These two methods are the fastest. On Chrome and Firefox the typeofversion is the fastest. On Internet Explorer the instanceof version was the fastest. But the difference in speed between these two methods were minor. Personally I’d still go with the instanceof method if I could because it’s cleaner.

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

Sidebar

Related Questions

what's the quickest way to extract a 5 digit number from a string in
What is the quickest way to find the number of elements in a static
What is the quickest and best performance way to turn a string into a
What's the quickest and easiest way to convert my json, containing the data of
What’s the quickest way to export SQL Server data to CSV with ALL column
What is the quickest way to get a large amount of data (think golf)
What is the quickest way in ColdFusion (or Java) for converting a string as
What's the quickest way to sort items by number of occurrences on a Linux
In PHP what is the quickest way to convert an mobile number to international
What is the quickest way to put up a mobile-friendly page displaying data pulled

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.