x


Where can I find a list of variable types for use in Javascript?

Where can I find a list of variable types so I can type ("var mynum:float;") all my variables?

more ▼

asked Dec 17 '10 at 10:38 PM

davedev gravatar image

davedev
594 38 41 49

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You generally don't need to do that; type inference takes care of it for you.

var foo = 5.0;         // This is typed as a float
var foo = 5;           // This is typed as an int
var foo = Vector3.zero // This is typed as a Vector3

Pretty much the only way to get an untyped variable is to do this:

var foo;

If you're declaring a variable with just the type and default value ("var foo : float;"), then this has a list of some of the types.

more ▼

answered Dec 17 '10 at 11:11 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

I was thinking mostly of class variables and I'd rather have them be null rather than come up with some default value.

Dec 18 '10 at 03:52 PM davedev
(comments are locked)
10|3000 characters needed characters left

The most common built in are:

  • boolean (true/false)
  • int (numbers without decimals)
  • float (numbers with decimals)
  • String (text)

Obviously there are more, but these are the basic ones everyone should be familiar with...

Then there are a ton of types in the mono framework. I am not really a mono guy myself, I look things up in MSDN for .NET docs.

Finally Unity expose several types that you find in the scripting reference, stuff like Vector3, Quaternion, MonoBehaviour, Time...

  • This all comes from someone who doesn't use Javascript on a regular basis so I'd love to see JS people chip in with their thoughts and point good Dave here to better resources.
more ▼

answered Dec 17 '10 at 11:14 PM

Statement gravatar image

Statement ♦♦
20.2k 35 71 176

The types in C# and JS are 99% the same; the only differences I know of offhand are "bool" vs "boolean" and "string" vs "String".

Dec 17 '10 at 11:17 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

I found this page that lists types used in UScript I have a case where float type fails a comparison, so I was looking for single or double... seems double is the only one available.

more ▼

answered Apr 30 '11 at 08:54 AM

dingben gravatar image

dingben
359 26 31 41

Yes, that's the same page I linked to in my answer. ;) Not sure what you mean by "double is the only one available", since you can see on that page that both single (AKA float) and double exist.

Apr 30 '11 at 09:06 AM Eric5h5

I like float. The power to remain accurate at the nano level, while avoiding the overkill at Galactic sizes is indeed a plus. I am probably not as versed as you on the way floating point arithmetic is performed on the various processors with all the bit shifting and swapping tricks and the implicit conversions that are performed at the compile and hardware levels. My statement aims to keep in mind that a float even if considered a Single in some(perhaps most) contexts, it is not the same as a Single. Depending on the requirement of a particular calculation, a float may eat up more instructions cycles than a 'pure' Single/Double, in some cases twice as much. ...and I know, the general consensus on today's machines(above 486) is that float should be faster or at least equal(except for division perhaps), while Single generally outperforms Double but not always. But I still want to keep the difference in mind. At some point, I may run into the last level of optimization that will require to understand precisely how the numeric types are handled at the heart of the system to know what to use where. That of course is mainly required when an operation must be performed constantly(repeatedly) in a fast moving concurrent scenario. ...and you guessed it, I foresee that scenario is/(will be) in my project. Having said that, I admit I am on shifty ground based on mostly educated guessing.

Jun 24 '11 at 03:45 AM dingben

a float even if considered a Single in some(perhaps most) contexts, it is not the same as a Single

float is an alias for System.Single. It's exactly the same.

Jun 24 '11 at 03:58 AM Eric5h5

I dislike using float. I use decimals whenever precision is of paramount importance, and typecast to float only at the very last second if the value absolutely needs to be a float.

Jun 24 '11 at 04:03 AM Dreamblur

Sorry... for my ambiguity. I am stuck in the pre-1990s where 'Single' refers to 'Single Precision FIXED point'...in those days a Single or a Double were fixed point... in today's meaning float implies Single Precision Float usually, double Float -> Double Precision Float. Thus where I used 'Single', I meant Single Precision FIXED point.

Jun 28 '11 at 08:35 AM dingben
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x847
x135

asked: Dec 17 '10 at 10:38 PM

Seen: 3103 times

Last Updated: Jun 28 '11 at 08:35 AM