3.2. Variable Types in JavaScript

When working with any programming language, you as a developer need to be concerned with the data types that you are working with. C# is a strongly typed language. This means that whenever you are going to work with a specific data point, you first need to cast the object you are working with to a specific data type. For example, to work with a string in C#, you are going to have to use something like the following bit of code:

string myString = "Hello World!";

JavaScript, on the other hand, is often characterized as having no data types. Everything in JavaScript is just an object. This is basically a true statement; however, some primitive types can be assigned to variables, and the variables can pick up those types (this is similar to variants for those of you with COM experience). Numbers, strings, Booleans, and functions are the primitive types in JavaScript. They are the building blocks for all objects in JavaScript. Even the higher order "types" that are predefined, such as arrays and regular expressions, are just collections of the basic types.

Listing 3-7 declares a few variables, assigns them integer values, and then displays true or false after treating them as Boolean values. The key thing to note is that JavaScript follows the C-style convention of treating 0 as false and everything else as true. When the page is run, the result is to display true for one, false for zero, and true for negative one.

Example 3-7. Working with Booleans ...

Get Professional ASP.NET 3.5 AJAX now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.