![]() Although people might have a different interpretation of what is and isn’t a number, the string “four” is not a number to the isNaN() function, whereas the string “4” is. The string value “This is not a number” is obviously also not a number. For example, a number divided by zero would be an illegal number in JavaScript. You use the isNaN() function to determine whether a number is legal or valid according to the ECMA-262 specification. NaN is an abbreviation for Not a Number, and it represents an illegal number. Your usage might vary, but an explanation follows nonetheless. By common, I mean that isNaN() is a function that I use frequently in JavaScript programming. One more common numeric function is the isNaN() function. ![]() The European Computer Manufacturers Association (ECMA) standard defines several of them. JavaScript has some built-in functions (and objects, too, which you learn about soon) for working with numeric values. It’s interesting to note that even though you multiplied two hexadecimal numbers, the output in the alert dialog box is in base 10 format. The resulting variable is then passed to the alert() function, which displays the dialog box in the preceding step 3. Then a new variable is created and set to the product of the previous two variables, as follows: var j = h * i The preceding script first defines two variables (you learn about defining variables later in this chapter) and sets them equal to two hexadecimal numbers, 0xe (14 in base 10 notation) and 0x2, respectively: var h = 0xe You should see a dialog box similar to this one: Var h = 0xe var i = 0x2 var j = h * i alert(j) #Writeroom floating window code#Within the webpage, replace the TODO comment with the boldface code shown here: Using Microsoft Visual Studio, Eclipse, or another editor, edit the file example1.html in the Chapter04 sample files folder in the companion content. Performing hexadecimal math with JavaScript Hexadecimal numbers are valid in JavaScript, and you won’t be surprised to learn that JavaScript allows math to be performed using all of the listed number formats. The last example, 0xd, is a hexadecimal number. All these are perfectly valid numbers in JavaScript: 4 However, what might be a surprise for programmers who are familiar with data types in other languages like C is that integers and floating point numbers do not have special or separate types. Numbers in JavaScript are just what you might expect them to be: numbers. ![]() The Date and RegExp types are discussed in this chapter, and the Array type is discussed in Chapter 8. I examine each of the data types in turn and explain objects further in Chapter 8.Īdditionally, JavaScript has several reference data types, including the Array, Date, and RegExp types. The latter three-null, undefined, and objects-require some additional explanation. The first three data types-numbers, strings, and Booleans-should be fairly familiar to programmers in any language. ![]() The six data types in JavaScript discussed in this chapter are as follows: (The answer depends largely on the definition of a data type.) You work with all these data types regularly, some more than others. Depending on who you ask, JavaScript defines anywhere from three to six data types. You’re probably already familiar with data types, such as strings or integers, from other languages. The data types of a language describe the basic elements that can be used within that language. Use functions associated with the data types. Understand the primitive data types used in JavaScript. After completing this chapter, you will be able to ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |