Summary - CGS3066: Web Programming and Design Summer 2014
ull is used for representing the intentional absence of an object value and is a primitive value. Unlike undefined, it is not a property of the global object. It is equal to undefined but not identical to it. null == undefined; // true null === undefined; // false CAREFUL: The typeof null is 'object'. typeof null; // 'object'; To properly check if a value is null, compare it with the strict equality operator var a = null; a === null; // true Section 3.2: Testing for NaN using isNaN() NaN() The global function isNaN() can be used to check if a certain value or expression evaluates to NaN. This function (in short) first checks if the value is a number, if not tries to convert it (*), and then checks if the resulting value is NaN. For this reason, this testing method may cause confusion. (*) The "conversion" method is not that simple, see ECMA-262 18.2.3 for a detailed explanation of the algorithm. These examples will help you better understand the isNaN() behavior: isNaN(NaN); // true isNaN(1); // false: 1 is a number isNaN(-2e-4); // false: -2e-4 is a number (-0.0002) in scientific notation isNaN(Infinity); // false: Infinity is a number isNaN(true); // false: converted to 1, which is a number isNaN(false); // false: converted to 0, which is a number isNaN(null); // false: converted to 0, which is a number isNaN(""); // false: converted to 0, which is a number isNaN(" "); // false: converted to 0, which is a number isNaN("45.3"); // false: string representing a number, converted to 45.3 isNaN("1.2e3"); // false: string representing a number, converted to 1.2e3 isNaN("Infinity"); // false: string representing a number, converted to Infinity isNaN(new Date); // false: Date object, converted to milliseconds since epoch isNaN("10$"); // true : conversion fails, the dollar sign is not a digit isNaN("hello"); // true : conversion fails, no digits at all isNaN(undefined); // true : converted to NaN isNaN(); // true : converted to NaN (implicitly undefined) isNaN(function(){}); // true : conversion fails isNaN({}); // true : conversion fails isNaN([1, 2]); // true : converted to "1, 2", which can't be converted to a number This last one is a bit tricky: checking if an Array is NaN. To do this, the Number() constructor first converts the array GoalK – JavaScript® Notes for Professionals 13 to a string, then to a number; this is the reason why isNaN([]) and isNaN([3
Written for
- Institution
- CGS3066: Web Programming and Design Summer 2014
- Course
- CGS3066: Web Programming and Design Summer 2014
Document information
- Uploaded on
- July 8, 2024
- Number of pages
- 490
- Written in
- 2023/2024
- Type
- SUMMARY
Subjects
-
javascript notes for professionals