As defined earlier, JavaScript is a loosely typed language, which really means that you don't have to be concerned about what kind of data is stored in a variable. You can assign a number to x on one line and on the next line assign a string to x , you can compare numbers and strings, strings and Booleans, and so on. JavaScript automatically converts values when it assigns values to a variable or evaluates an expression. If data types are mixed (i.e., a number is compared with a string, a Boolean is compared with a number, a string is compared with a Boolean), JavaScript must decide how to handle the expression. Most of the time, letting JavaScript handle the data works fine, but there are times when you want to force a conversion of one type to another. For example, if you prompt a user for input, the input is set as a string. But, suppose you want to perform calculations on the incoming data, making it necessary to convert the strings to numbers . When using the + operator you want to add two numbers that have been entered as strings, not concatenate them, so you will then need to convert the data from string to number. JavaScript provides three methods to convert the primitive data types. They are:
Example 5.14<html> <head><title>The Conversion Methods</title></head> <body> <p> <h3>Data Conversion</h3> script language="JavaScript"> 1 var num1 = prompt("Enter a number: ",""); var num2 = prompt("Enter another number: ",""); 2 var result = Number(num1) + Number(num2); // Convert strings to numbers 3 alert("Result is "+ result ); 4 var myString=String(num1); 5 result=myString + 200; // String + Number is String 6 alert("Result is "+ result); // Concatenates 200 to the // result; displays 20200 7 alert("Boolean result is "+ Boolean(num2)); // Prints true </script> </body> </html> EXPLANATION
5.3.1 The parseInt() MethodThis method converts a string to a number. It starts parsing at the beginning of the string and returns all integers until it reaches a non-integer and then stops parsing. If the string doesn't begin with an integer, NaN [2] (not a number) is returned. For example, parseInt("150cats") becomes 150 , whereas parseInt("cats") becomes NaN . You can also use octal and hexadecimal numbers. In the two-argument format, the first argument to parseInt() is a string containing a number base (radix) ranging from 2 to 36. The default is base 10. In the statement, parseInt("17", 8) , the result is 15 . The first argument is the string to be parsed and the second argument, 8, is the number base of the number (here, octal 17). The value returned is decimal 15 . Refer to Tables 5.14 and 5.15.
FORMAT parseInt(String, NumberBase); Default base is 10 parseInt(String); Example: parseInt("111", 2); 7 ( 111 in base 2 is 7) parseInt("45days"); 45 Table 5.14. parseInt(String).
Table 5.15. parseInt(String, NumberBase).
Example 5.15<html> <head> <title>Using the parseInt() Function</title></head> <body><font face="arial size="+1"> <b> <script language = "JavaScript"> 1 var grade = prompt("What is your grade? ", ""); // Grade entered as a string 2 grade=parseInt(grade); // Grade converted to an integer 3 document.write("grade type is<em> " + typeof(grade)) ; 4 grade+=10; 5 document.write("<em><br>After a 10 point bonus, your grade is " + grade + "!<br>"); </script> </body> </html> EXPLANATION
5.3.2 The parseFloat() MethodThe parseFloat() method is just like the parseInt() method except that it returns a floating-point number. A floating-point [3] number is a number that contains a fractional part, such as 3.0, “22.5, or .15. The decimal point is allowed in the string being parsed. If the string being parsed does not start with a number, NaN (not a number) is returned.
FORMAT parseFloat(String); Example: parseFloat("45.3 degrees"); Table 5.16. parseFloat(String).
Example 5.16 <html> <head> <title>Using the parseFloat() Function</title> <script language = "JavaScript"> 1 var temp = prompt("What is your temperature? ", ""); 2 temp=parseFloat(temp); 3 if(temp == 98.6){ 4 alert("Your temp is normal"); } 5 else{ alert("You are sick!"); } </script></head><body></body></html> EXPLANATION
5.3.3 The eval() MethodThe eval() method evaluates a string of JavaScript statements and evaluates the whole thing as a little program, returning the result of the execution. [4] If there is no result, undefined is returned.
FORMAT eval(String); Example: eval("(5+4) / 3"); Example 5.17<html> <head> <title>The eval() Function</title> </head> <body bgcolor="lightblue"> <font size="+1" face="arial"> <script language="JavaScript"> 1 var str="5 + 4"; 2 var num1 = eval(str); 3 var num2 = eval(prompt("Give me a number ", "")); 4 alert(num1 + num2); </script> </body> </html> EXPLANATION
|