Automatic Conversion between Types : Number Data Type « Language Basics « JavaScript DHTML






Automatic Conversion between Types

 
<HTML>
<HEAD>
<TITLE>Implicit conversion between types</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
s1="test"
s2="1122.34"
i=123
r=.123
lt=true
lf=false
n=null
// -->
</SCRIPT>
</HEAD>
<BODY>
<H1>Implicit conversion between types</H1>
<TABLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write("<TR>")
document.write("<TH>row + column</TH>")
document.write("<TH>string \"12.34\"</TH>")
document.write("<TH>integer 123</TH>")
document.write("<TH>float .123</TH>")
document.write("<TH>logical true</TH>")
document.write("<TH>logical false</TH>")
document.write("<TH>null</TH>")
document.write("</TR>")
// First operand is a string
document.write("<TR>")
document.write("<TH>string \"test\"</TH>")
document.write("<TD>")
document.write(s1+s2)
document.write("</TD><TD>")
document.write(s1+i)
document.write("</TD><TD>")
document.write(s1+r)
document.write("</TD><TD>")
document.write(s1+lt)
document.write("</TD><TD>")
document.write(s1+lf)
document.write("</TD><TD>")
document.write(s1+n)
document.write("</TD>")
document.write("</TR>")
// First operand is an integer
document.write("<TR>")
document.write("<TH>integer 123</TH>")
document.write("<TD>")
document.write(i+s2)
document.write("</TD><TD>")
document.write(i+i)
document.write("</TD><TD>")
document.write(i+r)
document.write("</TD><TD>")
document.write(i+lt)
document.write("</TD><TD>")
document.write(i+lf)
document.write("</TD><TD>")
document.write(i+n)
document.write("</TD>")
document.write("</TR>")
// First operand is a float
document.write("<TR>")
document.write("<TH>float .123</TH>")
document.write("<TD>")
document.write(r+s2)
document.write("</TD><TD>")
document.write(r+i)
document.write("</TD><TD>")
document.write(r+r)
document.write("</TD><TD>")
document.write(r+lt)
document.write("</TD><TD>")
document.write(r+lf)
document.write("</TD><TD>")
document.write(r+n)
document.write("</TD>")
document.write("</TR>")
// First operand is a logical true
document.write("<TR>")
document.write("<TH>logical true</TH>")
document.write("<TD>")
document.write(lt+s2)
document.write("</TD><TD>")
document.write(lt+i)
document.write("</TD><TD>")
document.write(lt+r)
document.write("</TD><TD>")
document.write(lt+lt)
document.write("</TD><TD>")
document.write(lt+lf)
document.write("</TD><TD>")
document.write(lt+n)
document.write("</TD>")
document.write("</TR>")
// First operand is a logical false
document.write("<TR>")
document.write("<TH>logical false</TH>")
document.write("<TD>")
document.write(lf+s2)
document.write("</TD><TD>")
document.write(lf+i)
document.write("</TD><TD>")
document.write(lf+r)
document.write("</TD><TD>")
document.write(lf+lt)
document.write("</TD><TD>")
document.write(lf+lf)
document.write("</TD><TD>")
document.write(lf+n)
document.write("</TD>")
document.write("</TR>")
// First operand is null
document.write("<TR>")
document.write("<TH>null</TH>")
document.write("<TD>")
document.write(n+s2)
document.write("</TD><TD>")
document.write(n+i)
document.write("</TD><TD>")
document.write(n+r)
document.write("</TD><TD>")
document.write(n+lt)
document.write("</TD><TD>")
document.write(n+lf)
document.write("</TD><TD>")
document.write(n+n)
document.write("</TD>")
document.write("</TR>")
// -->
</SCRIPT>
</TABLE>
</BODY>
</HTML>

           
         
  








Related examples in the same category

1.Define variables, assign values and output
2.Dividing by Zero
3.Add Four Numbers from an HTML Form (and Display the Results)
4.Add characters
5.Complex class to represent complex numbers
6.A Function That Returns the Sum of Three Numbers (Stripped-Down Version)
7. Concatenating Variables and Displaying the Value Contained
8.JavaScript Loan Calculator
9.Factorials
10.Converting a Number to a String
11.Creating Number Objects Rather than Performing String-to-Number Conversions
12.Using toString() with Radix Values
13.Using JavaScript Integers
14.Conversion of Logical Values to Numeric Values
15.Using Floating-Point Numbers
16.Explicit Conversion Functions
17.Date Object Calculations
18.Operator Precedence and Different Data Types
19.Converting Base 10 to Base 16 Using Bitwise Operators
20.Number Calculation
21.Format a number
22.Format a number 2
23.Decimal to 2 Hex
24.Concatenate integer variable to a string variable