473,463 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Determining variable type

If I have a variable of some sort, say x, how can I find out what
variable type javascript thinks it is? For example, if I've declared x
as 5 earlier, how do I know now whether it's a string or a number,
isNaN returns false, regardless of whether I declared it as "5" or 5.

May 26 '07 #1
4 10290
Lee
mouseit said:
>
If I have a variable of some sort, say x, how can I find out what
variable type javascript thinks it is? For example, if I've declared x
as 5 earlier, how do I know now whether it's a string or a number,
isNaN returns false, regardless of whether I declared it as "5" or 5.
Javascript doesn't think it needs to know.
Why do you think you do?
--

May 27 '07 #2
mouseit wrote:
If I have a variable of some sort, say x, how can I find out what
variable type javascript thinks it is? For example, if I've declared x
as 5 earlier, how do I know now whether it's a string or a number,
isNaN returns false, regardless of whether I declared it as "5" or 5.
typeof or instanceof.

typeof will give the generic object type.
instanceof will only return true if it is an object of type [Object].
([Object] being any valid JavaScript Object; String, Function, Array,
Object, whatever.)

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 27 '07 #3
mouseit wrote:
If I have a variable of some sort, say x, how can I find out what
variable type javascript thinks it is? For example, if I've declared x
as 5 earlier, how do I know now whether it's a string or a number,
isNaN returns false, regardless of whether I declared it as "5" or 5.

typeof 5 === 'number'
typeof '5' === 'string'

http://javascript.crockford.com/survey.html
May 27 '07 #4
On May 26, 8:58 pm, Douglas Crockford <nos...@sbcglobal.netwrote:
mouseit wrote:
If I have a variable of some sort, say x, how can I find out what
variable type javascript thinks it is? For example, if I've declared x
as 5 earlier, how do I know now whether it's a string or a number,
isNaN returns false, regardless of whether I declared it as "5" or 5.

typeof 5 === 'number'
typeof '5' === 'string'

http://javascript.crockford.com/survey.html
Thank you all!

May 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Kamermans | last post by:
Is there a way to determine what a node() find returns? For instance, if I have a snip of XSLT that looks like: <xsl:for-each select="node()"> ... </xsl:for-each> Is there a way to, for each...
3
by: Trevor M. Lango | last post by:
The number of arguments passed to the following functions can be determined - but how is this coded? main (int argc, char *argv) printf ( string format ) I would like to be able to write...
3
by: Steve Brown | last post by:
Hello all, Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this: template <class T> Object::Object(int TypeCode, T* data)...
0
by: CTDev Team | last post by:
Hi, We are using Exchange Server 5.5, and have applications written in VB6 and C# that read and process emails. We are experiencing intermittent errors similar to C# Application ...
4
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
5
by: Doug Stiers | last post by:
C# newbie question: I'm trying to determine the type of a datareader value. Why cant I do it like this? if (rdr.GetValue(0).GetType() is Guid || rdr.GetValue(0).GetType() is Int32) { // do...
9
by: Schraalhans Keukenmeester | last post by:
I have some C functions (with variable length argument lists) that use void pointers as arguments. Is there a way to determine at runtime what type of parameter is actually passed on to the...
5
by: Jeremy | last post by:
Does anyone have a clever algorithm for generating an outline of the current document from (client-side) javascript using DOM methods? For example, let's say I predictably have a document...
2
by: pob | last post by:
I was all happy because thru some tutelage of others I created this array to find the newest file in a directory. However, I then realized its great that I now know the latest file, but I need to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.