JavaScript (Part 3)- Type Conversion, Comparison Operators, And Control Structures
JavaScript (Part 3)- Type Conversion, Comparison Operators, And Control Structures
Ullrich Hustadt
Type Coercion
• JavaScript automatically converts a value to the appropriate type as
required by the operation applied to the value (type coercion)
5 * "3" // result is 15
5 + "3" // result is "53"
5 && "3" // result is "3"
Type Coercion
When converting to boolean, the following values are considered false:
• the boolean false itself
• the number 0 (zero)
• the empty string, but not the string ’0’
• undefined
• null
• NaN
Type Casting
JavaScript provides several ways to explicitly type cast a value
• Apply an identity function of the target type to the value
"12" * 1 ; 12 !!"1" ; true
12 + "" ; "12" !!"0" ; true
false + "" ; "false" !!"" ; false
[12,[3,4]] + "" ; "12,3,4" !!1 ; true
[12,13] * 1 ; NaN
[12] * 1 ; 12
Type Casting
JavaScript provides several ways to explicitly type cast a value
• Wrap a value of a primitive type into an object
; JavaScript has objects Number, String, and Boolean with
unary constructors/wrappers for values of primitive types
(JavaScript does not have classes but prototypical objects)
Number("12") ; 12 Boolean("0") ; true
String(12) ; "12" Boolean(1) ; true
String(false) ; "false" Number(true) ; 1
• Use parser functions parseInt or parseFloat
parseInt("12") ; 12 parseFloat("2.5") ; 2.5
parseInt("2.5") ; 2 parseFloat("2.5e1") ; 25
parseInt("E52") ; NaN parseFloat("E5.2") ; NaN
parseInt(" 42") ; 42 parseFloat(" 4.2") ; 4.2
parseInt("2014Mar") ; 2014 parseFloat("4.2end") ; 4.2
Comparison Operators
JavaScript distinguishes between (loose) equality ==
and strict equality ===:
expr1 == expr2 Equal TRUE iff expr1 is equal to expr2
after type coercion
expr1 != expr2 Not equal TRUE iff expr1 is not equal to expr2
after type coercion
• When comparing a number and a string, the string is converted to a
number
• When comparing with a boolean,
the boolean is converted to 1 if true and to 0 if false
• If an object is compared with a number or string, JavaScript uses the
valueOf and toString methods of the objects to produce a primitive
value for the object
• If two objects are compared, then the equality test is true only if both
refer to the same object
Comparison Operators
JavaScript distinguishes between (loose) equality ==
and strict equality ===:
expr1 === expr2 Strictly equal TRUE iff expr1 is equal to expr2,
and they are of the same type
expr1 !== expr2 Strictly not TRUE iff expr1 is not equal to expr2,
equal or they are not of the same type
Comparison Operators
JavaScript’s comparison operators also apply type coercion to their
operands and do so following the same rules as equality ==:
expr1 < expr2 Less than true iff expr1 is strictly less than expr2
after type coercion
expr1 > expr2 Greater than true iff expr1 is strictly greater than expr2
after type coercion
expr1 <= expr2 Less than true iff expr1 is less than or equal to expr2
or equal to after type coercion
expr1 >= expr2 Greater than true iff expr1 is greater than or equal to expr2
or equal to after type coercion
Control Structures
do
statement
while ( condition )
Example:
// Compute the factorial of a given number
var factorial = 1;
do {
factorial *= number - -
} while ( number > 0)
var factorial = 1
for ( var i = 1; i <= number ; i ++)
factorial *= i
Error handling
• When a JavaScript statement generates an error, an exception is thrown
• Exceptions can also explicitly be thrown via a throw statement
• A try ... catch ... statement allows for error / exception handling
try { statements }
catch ( error ) { statements }
finally { statements }
Error handling
• When a JavaScript statement generates an error, an exception is thrown
• Exceptions can also explicitly be thrown via a throw statement
• A try ... catch ... statement allows for error / exception handling
throw expression
x = "A"
try {
if ( isNaN ( x )) throw " x is NaN "
y = x . toFixed (2)
} catch ( e ) {
console . log ( ’ Caught : ’ + e )
y = 0
} finally {
console . log ( ’ y = ’,y )
}
Caught TypeError : x . toFixed is not a function
y = 0
Coding Styles