473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ 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 10302
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...@sbcglob al.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
1519
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 node found, generate what kind of node it found, like <some_element> or <#PCDATA> or something that indicates the type of element it found?
3
1741
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 arbitrary functions that know how many arguments were passed to them.
3
1920
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) { switch (TypeCode) case 1:
0
1685
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 System.Runtime.InteropServices.COMException (0x80004005): The client
4
2155
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 broken in Firefox 1.5.0.1 on OS X. What happens with the Scriptaculous library is this In the html document the author only has to include one line
5
1807
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 something }
9
2784
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 function? PHP and my oldskool turbopascal provide a typeof() function, but my C compiler (gcc 3.4.1) does not seem to provide this function. Perhaps someone crafted a library with some smart code able to inspect the variable passed to a certain...
5
1575
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 structured hierarchically with <h1>...<h6tags. I want to generate an outline of the document wherein I have nested lists of the contents of the headers. Take for example the following snippet of a fictional legal document: ------------ <h1>Main...
2
2076
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 know the name of the file. Unfortunately, I am only passing the date into the array. Do I need to create a m-d array ? If so how do I create a 2-d array where I do not know how many rows I will be looping thru ?
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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 we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.