Is this Possible to give String data type as Number in JavaScript

98
February 14, 2022, at 00:00 AM

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

Answer 1

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...).

Answer 2

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)

READ ALSO
Selectively color radio button background if certain option selected

Selectively color radio button background if certain option selected

I'm looking for advice on how to selectively style with CSS, HTML or Javascript my set of radio buttons to have different background colors depending on which choice is selectedfor example, I would like technology score to have a background color of yellow...

77
Double-notched corner in CSS

Double-notched corner in CSS

Is it possible to produce the following shape containing a number with CSS:

93
centering a div in a grid item

centering a div in a grid item

I've been trying to horizontally center a div on the screen of an IphoneThe div is a bit narrower than the phone screen

61
How to connect one css with multiable html pages

How to connect one css with multiable html pages

So I new to html and want to make a website with 2 pages a home page and a contact pageand I just want to know how do you use the same Style

102