Click here to Skip to main content
14,883,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good afternoon JavaScript types. I have a simple query perhaps you could explain to my thick brain.

outputs 'number'. Well of course it does.

outputs 'string'. Now I'm confused, but if I do this:

I get back to 'number' again. These last two things are identical according to my C# eyes, so there's clearly something probably quite simple I'm missing here. Why does the second example print out 'string'?

What I have tried:

I object to being forced into telling what I've tried.
Updated 2-Apr-20 2:49am

Right, answered my own question. The second one is the equivalent of

console.log(typeof ((100).toString()))

Because in JavaScript the brackets appear optional when using typeof. I think I need a bucket to be sick in.
Richard Deeming 2-Apr-20 8:46am
Yes - typeof is an operator, not a function. :)
typeof - JavaScript | MDN[^]
Rob Philpott 2-Apr-20 8:54am
Perhaps it's just a rite or passage for those arriving at JavaScript from proper languages, but its really making me feel quite ill.
Richard Deeming 2-Apr-20 8:56am
Wait until you start trying to work out what this points to. :)

this - JavaScript | MDN[^]
OriginalGriff 2-Apr-20 9:06am
Welcome to Hell - I'd run for the doors now if I was you ...
I understand it is confusing; the easy explanation is that is an order of operations, and the type-conversion is done before the type-evaluation.

I spaced it out to show you how this is being evaluated in your last 2 lines of codes
console.log( typeof         (100).toString()  );
console.log( (typeof(100))  .toString()       );
typeof is not a function, it's an operator.

try :
typeof 100
typeof 100.toString() // error
typeof (100).toString()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900