WebReference.com - Chapter 2 of JavaScript Design, from New Riders (5/5) | WebReference

WebReference.com - Chapter 2 of JavaScript Design, from New Riders (5/5)

To page 1To page 2To page 3To page 4current page
[previous]

JavaScript Design

A Weakly Typed Language Means That JavaScript Is Smart

One last characteristic of JavaScript needs to be discussed before going on to the next chapter. JavaScript is considered a "weakly typed" or "untyped" language. The type in question here are the data types¬ónothing to do with typing from your keyboard. For programmers coming from C++ or Java, two strongly typed languages, this means that JavaScript will figure out what type of data you have and make the necessary adjustments so that you don't have to redefine your different types of data. Designers new to programming will welcome a weakly typed language because it will save time in learning several different conversion steps and data type declarations.

For example, suppose that you're setting up an e-business site with lots of financial figuring that you want to slap a dollar sign on when all of the calculations are finished. Performing calculations involving money requires floating-point numbers. However, as soon as a dollar sign is added to the number, it becomes a string variable. In strongly typed languages, you need to convert the floating-point number to a string and then concatenate it with the dollar sign. Consider the following example using JavaScript to add five different items and placing the whole thing into a string.

WeaklyType.html

<html>
<head>
<script language="JavaScript">
var apples=1.43;
var oranges=2.33;
var pears=4.32;
var tax=.04;
var shipping=2.75;
var subtotal=apples + oranges + pears;
var total=subtotal + (subtotal * tax) + shipping;
var message="Your total is $";
var deliver= message + total +".";
document.write(deliver);
</script>
</head>
<body bgcolor="indianred">
</body>
</html>

Of course, when the calculations are all complete, you could drop the decimal points to two, as was done in the example script VarFuncCase.html in the previous section. However, the point is that no type definitions were required. Strings and numbers were merrily mixed with no cause for concern. Although the term "weakly typed" might imply some deficiency with JavaScript, the term actually means that JavaScript is smart enough to do the work of determining what type of data any given variable should be. In learning about data types and variables in the next chapter, you will be very grateful that JavaScript (and not you) does most of the work in figuring out data types.

Summary

The purpose of this short chapter has been to get you off on the right foot when you begin writing your own scripts. The most frustrating experience in learning how to program a new language is debugging it when the outcome is not the one expected. By paying attention to certain details and learning a set of rules for writing JavaScript, not only are you less likely to run into bugs when you create a page that includes JavaScript, but you are more likely to locate and correct them when they do pop up.


To page 1To page 2To page 3To page 4current page
[previous]

Copyright Pearson Education and
Created: December 10, 2001
Revised: December 10, 2001

URL: http://webreference.com/programming/javascript/jsdesign/chap2/5.html