5

I would like to know if there is any way of overriding the behaviour of the typeof operator. Specifically, I want to return "string" when the typeof operator is called both on foo = "hi" and bar = new String("hi").

typeof bar returns "object" but I want it to return "string".

I know this can be done by declaring my own function or accessing the constructor name but I want to modify the behaviour of typeof operator.

EDIT - I am looking for some code that I can add say at the beginning of the program which modifies the behaviour of all the typeof operators in the rest of the program.

8
  • 3
    I'm 99% sure you can't override typeof...just create your own function Commented Jun 27, 2013 at 2:50
  • Thought as much..just wanted to be sure..thanks! Commented Jun 27, 2013 at 2:52
  • No problem! Not sure if someone can back me up with documentation, but... Commented Jun 27, 2013 at 2:54
  • 1
    Why don't you do typeof obj.valueOf()? It'll return a string primitive for both. Commented Jun 27, 2013 at 2:57
  • @ian looks like typeof is an operator so don't think you can change that: ecma-international.org/ecma-262/5.1/#sec-11.4.3 Commented Jun 27, 2013 at 2:58

4 Answers 4

5

That is impossible. The behaviour of native operators cannot be changed.

Related links:

Sign up to request clarification or add additional context in comments.

Comments

2

You can't change a Javascript operator, however you can check if it's a string OR a string object with instanceof.

var strObj = new String('im a string')
var str = 'im a string'

alert(strObj instanceof String); //true
alert(typeof strObj == 'string'); //false
alert(str instanceof String); //false
alert(typeof str == 'string'); //true
alert(strObj instanceof String || typeof strObj == 'string'); //true
alert(str instanceof String || typeof str == 'string'); //true

Of course, it is much more simple and shorter to create your own function, but if you want to use native JS, that is the way : alert(str instanceof String || typeof str == 'string');.

5 Comments

The same thing happened to me. I think there's a trigger happy kid here. People should really not be allowed to downvote an answer which has no other downvotes without posting a comment explaining why they downvoted the answer.
@AaditMShah Totally agree with you. IMO, there is only 3 type of answer: Good which are upvoted; Working which are, like mine, to be upvoted if the user is kind; and wrong who deserve a downvote. As i can see, both of our answers are working...
I dont think both your answers answer the question to the point. As I said "I am looking for some code that I can add say at the beginning of the program which modifies the behaviour of all the typeof operators in the rest of the program." Again, I did not downvote yours either.
@everconfusedGuy, I understand, but they actually clearly answer your question (wich is no) and give a little explaination (can't change javascript operator). In addition, we add an alternative. We both know the question was answered correctly, we are just adding some true information and we are wondering why we are getting a downvote (since it's true, you know?). Anyway, the downvoter will probably never come back here so it is a lost cause!
You can change the behaviour of the instanceof operator (now, as this was 2013, the answer might have been true).
1

typeof is an operator in JavaScript so I'm quite sure you can't. To detect if something is a string you could use something like this:

var s = "hello";
console.log(s.substr&&s.charAt&&s.toUpperCase==="".toUpperCase)//true
s = new String("hello");
console.log(s.substr&&s.charAt&&s.toUpperCase==="".toUpperCase)//true

Comments

0

No, you can't modify the behavior of typeof or any other operator for that matter. However the next best solution is to use Object.prototype.toString as follows:

function typeOf(value) {
    return Object.prototype.toString.call(value).slice(8, -1);
}

Now you can use it as follows (see the demo - http://jsfiddle.net/CMwdL/):

var foo = "hi";
var bar = new String("hi");

alert(typeOf(foo)); // String
alert(typeOf(bar)); // String

The reason this works is given in the following link: http://bonsaiden.github.io/JavaScript-Garden/#types.typeof

2 Comments

I knew how to get around this if i was can declare my own function (as mentioned in my question). I just wanted to know if I could modify the behaviour of the typeof operator itself
@naini No, you can't modify the behavior of typeof. However my answer is the best solution there is. I know that for a fact. Period.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.