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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:34:17+00:00 2026-05-28T14:34:17+00:00

I was messing around with JavaScript, and noticed that this can never be a

  • 0

I was messing around with JavaScript, and noticed that this can never be a primitive. What am I talking about? Let me explain.

Take this function for example.

function test(){
    return typeof this;
}
test.call('Abc'); // 'object'
test.call(123); // 'object'

They are both 'object', not 'string' or 'number', like I’d expect.

After a bit of confusion (and messing with instanceof), I figured out what’s going on. 'Abc' is being coverted to a String object, and 123 is being converted to a Number object.

Anyway, my question is why does this happen, and how do I convert an object back to its primitive?

I know I could use (String)this or (Number)this, but how can I do that if I don’t know the type?

EDIT: I was trying to do this:

function element(){
    var $e = $(this),
    $d = $e.closest('div');
}
element.call('#myID');

and it wasn’t working. this is a String object, and jQuery just made a collection of objects instead of using the selector to search the DOM.

  • 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-28T14:34:18+00:00Added an answer on May 28, 2026 at 2:34 pm

    As others noted, it’s coerced to an object as per the spec.

    Important thing to note is that if you’re in strict mode, the coercion doesn’t happen.

    "use strict";
    
    function test(){
        return typeof this;
    }
    test.call('Abc'); // 'string'
    test.call(123); // 'number'
    

    So the real question is why aren’t you using strict? 😉


    As you noted in your comment, you should be able to use .valueOf() if you’re supporting implementations that don’t support strict mode.

    If you’re only expecting a String, or if you’re also expecting a Number, but you don’t mind a numeric String instead, you could do this…

    (this + '') // "Abc"
    (this + '') // "123"
    

    “but how can I do that if I don’t know the type”

    If you want to know its type, use the generic toString available on Object.prototype to get the internal [[Class]] property.

    Object.prototype.toString.call( this ); "[object String]"
    Object.prototype.toString.call( this ); "[object Number]"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While I was messing around with truth tables in JavaScript, I noticed that the
Im just messing around with Ruby on Rails and HTML. This question isn't about
I'm reading crockford's Javascript: The Good Parts and am messing around with this piece
Hi I'm trying to learn javascript and I'm messing around with this example I
I was messing around with RhinoMocks this morning and couldn't run my tests because
Yeah so I've been messing around with javascript a while but only recently got
Can someone help me fill in the blanks with native JavaScript? function getHeading(lat1, lon1,
I was playing around with some JavaScript code and found that when I took
I have a simple javascript function that takes two variables. I need to pass
I want to start messing around with o3d but I can't find where to

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.