Skip to content
csusbdt edited this page Sep 14, 2014 · 15 revisions

Overview

The purpose of this assignment is to improve your understanding of type coercion in Javascript.

A good source of information about Javascript is available through the Mozilla Developer Network.

Assignment folder

Create folder ~/322/equal to hold the results of your work on this assignment. When you are finished with this assignment, this folder should contain the following file.

equality.html

Instructions

Javascript has 2 equality operators: == and ===. The operator == tries to coerce its operands to the same type and then checks equality. The operator === does not try coercion. For example, the number 0 can be coerced into boolean false. As a result, the following expression evaluates to true.

0 == false  // true

However, the following is false because === does no type coercion.

0 === false  // false

The following code shows how you should display the results of the above expression in a Web page for this assignment.

<script>
    document.writeln(0 == false);
    document.writeln(!(0 === false));
</script>

Select the correct comparison operators to make every call to writeln in your Web page return true.

We can simplify the last line of the above code with the knowledge that != is the negation of == and !== is the negation of ===.

    document.writeln(0 !== false);

The following table contains pairs of expressions that you should investigate for type-coerced equality (== and !=) and strict equality (=== and !==). In the table LHS is left-hand side and RHS is right-hand side of the equality operator.

LHS RHS
0 false
1 true
3 true
0 null
0 undefined
0 ""
0 '0'
0 'false'
3 '3'
true 'true'
false 'false'
null false
null undefined
null 0
undefined window.x1234
undefined typeof(x1234)
'undefined' typeof(x1234)
{ x: 0 } { x: 0 }
'a' 'a'
a where var a = 0; b where var b = 0;
c where var c = { x: 0 }; d where var d = { x: 0 };
c where var c = { x: 0 }; e where var e = c;

Create a file named equality.html that demonstrates the type-coercing and non-type-coercing equality between each pair of expressions in the table. Your file would start as follows and continue to include 2 calls to writeln for each row in the table. Select the correct comparison operators to make every call to writeln return true.

<script>
document.writeln("<br>Compare 0 and false: ");
document.writeln(0 == false);
document.writeln(0 !== false);

document.writeln("<br>Compare 1 and true: ");
document.writeln(1 == true);
document.writeln(1 !== true);

document.writeln("<br>Compare 3 and true: ");
// ...
// ...
</script>

When viewed in a browser, your Web page should look like the following.

Compare 0 and false: true true 
Compare 1 and true: true true 
Compare 3 and true: true true 
Compare 0 and null: true true 
Compare 0 and undefined: true true 
Compare 0 and string: true true 
Compare 0 and '0': true true 
Compare 0 and 'false': true true 
Compare 3 and '3': true true 
Compare true and 'true': true true 
Compare false and 'false': true true 
Compare null and false: true true 
Compare null and undefined: true true 
Compare undefined and window.x1234: true true 
Compare undefined and typeof(x1234): true true 
Compare 'undefined' and typeof(x1234): true true 
Compare { x: 0 } and { x: 0 }: true true 
Compare 'a' and 'a': true true 
Compare a where var a = 0 and b where var b = 0: true true 
Compare c where var c = { x: 0 }; and d where var d = { x: 0 };: true true 
Compare c where var c = { x: 0 }; and e where var e = c;: true true

Note: Add the following to your code before the comparisons between a, b, c, d and e.

var a = 0;
var b = 0;
var c = { x: 0 };
var d = { x: 0 };
var e = c;

FYI

For your information, the following expression throws a ReferenceError (assuming that x1234 is not defined).

x1234 === undefined

However, the following expression does not throw an exception.

window.x1234 === undefined

In browsers, window is a built-in variable that references Javascript's global object. If you want to test for the existence of x1234 in global scope without causing an exception, you can either refer to x1234 as a property of window, such as

window.x1234 === undefined

or use the typeof function, such as

typeof(x1234) === 'undefined'

Referring to x1234 directly in global scope results in a ReferenceError.

try {
    x1234 === undefined;
} catch(e) {
    document.writeln(e instanceof ReferenceError); // writes true
}
Clone this wiki locally