Can i give string data type as Number For example
var a = "abc"
//Some Code
console.log(typeof a) //number
I want to play with "typeof" i want to print string as a number WITHOUT changing it to number
CodePudding user response:
JavaScript doesn't provide any means of overriding the typeof operator, so you can't do that.
In fact, JavaScript doesn't have operator overloading at all (and is extremely unlikely ever to get it). There are a couple of games you can play with toString and valueOf on object instances, but they don't really provide overloading, and don't relate to typeof (and your string is a primitive string, not an object, so...).
CodePudding user response:
No, you can't effectively overload typeof (or any of the other JavaScript operators) like that. If an expression is a string, its typeof will always give you string. If an expression is a number, its typeof will always give you number. There's no way to change that.
(You can create objects with new Number and new String, which will then give you object instead, but those constructors should never be used, because then you'll have objects, not primitives, which will behave in unexpected ways)
