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 7604007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:47:52+00:00 2026-05-30T23:47:52+00:00

Simplest fool proof(?) method to check for string object I’ve seen. Tested with many

  • 0

Simplest fool proof(?) method to check for string object I’ve seen. Tested with many different objects types. Are there parameters/circumstances that might trip it up, and are there simper functions available?

function isString(o){
    if(o == null || o == undefined){
        return false;
    }
    if(typeof(o) == 'string'){
        return true;
    }
    if(typeof(o) == 'object'&& typeof(o.valueOf) == 'function' && typeof(o.valueOf()) == 'string'){
        return true;
    }
    return false;
}

Here’s what I’ve check with:

<html>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<input id="el">
<script type="text/javascript">

function isString(o){
    if(o == null || o == undefined){
        return false;
    }
    if(typeof(o) == 'string'){
        return true;
    }
    if(typeof(o) == 'object'&& typeof(o.valueOf) == 'function' && typeof(o.valueOf()) == 'string'){
        return true;
    }
    return false;
}
function tellIfString(o){
    if(isString(o)){
        return "A string!<br />";
    }else{
        return "Not a string!<br />";
    }
}
function CustomO(a){
    this.a = a;
}
literal = 'string';
objectWrapped = new String('why do i feel different?');
stringNumber = '345';
number = 234;
numberWrapped = new Number(3345);
object = {};
array = [];
bool = true;
nan = 1*'sdf';
regex = /woop/;
regex_o = new RegExp('asasd');
o_no_valueOf = {};
o_no_valueOf = delete(o_no_valueOf.valueOf);
customO = new CustomO({});
document.write("literal Is "+tellIfString(literal));
document.write("objectWrapped string Is "+tellIfString(objectWrapped));
document.write("stringNumber Is "+tellIfString(stringNumber));
document.write("number Is "+tellIfString(number));
document.write("numberWrapped Is "+tellIfString(numberWrapped));
document.write("object Is "+tellIfString(object));
document.write("array Is "+tellIfString(array));
document.write("bool Is "+tellIfString(bool));
document.write("nan Is "+tellIfString(nan));
document.write("null Is "+tellIfString(null));
//document.write("notdefined Is "+tellIfString(notdefined));//don't need to check variables that haven't been defined, they error before they get to the function
document.write("undefined Is "+tellIfString(undefined));
document.write("Math Is "+tellIfString(Math));
document.write("function Is "+tellIfString(function(){}));
document.write("regex Is "+tellIfString(regex));
document.write("regexO Is "+tellIfString(regex_o));
document.write("o_no_valueOf Is "+tellIfString(regex_o));
document.write("customO Is "+tellIfString(customO));
document.write("jQuery return Is "+tellIfString($('#el')));
document.write("document Is "+tellIfString(document));//this case is important because it requires the typeof(o.valueOf) == 'function'
document.write("window Is "+tellIfString(window));
document.write("document.cookie Is "+tellIfString(document.cookie));
document.write("window.open Is "+tellIfString(window.open));
document.write("window.location Is "+tellIfString(window.location));
document.write("All objects evaluated!");

</script>

</body>
</html>

See also(especially the parts about new String and .valueOf()
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String

  • 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-30T23:47:53+00:00Added an answer on May 30, 2026 at 11:47 pm

    I think it’s more sensible to write:

    function isString(o)
        { return typeof(o) == 'string' || o instanceof String; }
    

    Edited to add: This is not absolutely foolproof; Felix Kling, in the comments, points out one case where it will not work. Based on this page, I believe that this:

    function isString(o)
        { return Object.prototype.toString.call(o) === '[object String]'; }
    

    is “more” foolproof. (I’ve found that you can still break it by messing with Object.prototype.toString, and perhaps in other ways, but if you distrust your own code that much, then there’s really nothing you can do.)

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

Sidebar

Related Questions

What is the simplest way to check if a string contains newline? For example,
What's the simplest-to-use techonlogy available to save an arbitrary Java object graph as an
What is the simplest way to replace quote characters with \ sequence inside string
What is the simplest way to use database persistence in Java? I know, many
The simplest script: <?php echo 'hello'; Takes about 3 seconds to execute. There seems
What's the simplest way to convert a java.sql.Date object to a java.util.Date while retaining
What's the simplest way to get the String representation of a XML Document (
What is the simplest way to define setter and getter in Python? Is there
The simplest example would be the built in class keyValuePair(of T,U). I would like
The simplest way to deal with python package installations, so far, to me, has

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.