Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View Essentials of Javascript - Cultural View
JavaScript syntax 23 Boolean JavaScript provides a Boolean data type with true and false literals. The typeof operator returns the string "boolean" for these primitives. In a logical context, automatic type coercion evaluates 0, -0, null, NaN, undefined or the empty string ("") as false. Everything else evaluates as true, including the strings "0", "false" and all objects. Automatic coercion can sometimes be avoided by using the type checked comparison operators, (=== and !==). // Automatic type coercion alert(true == 1); // true.... Only ±0 and NaN are false Numbers. alert(true == "0"); // true.... Only the empty String is false. // Type checked comparison alert(true === 1); // false... The data types don't match. alert(true === "0"); // false... The data types don't match. The binary logical operators returned a Boolean value in early versions of JavaScript, but now they return one of the operands instead. The left–operand is returned if it can be evaluated as: false, in the case of conjunction (a && b), or true, in the case of disjunction (a || b); otherwise the right–operand is returned. An expression can be explicitly cast to a Boolean primitive by: doubling the logical negation operator (!!), using the Boolean() function, or using the conditional operator (c ? t : f). alert({} || null); // Object {}... Any Object is true. alert(!0 && null); // null........ The negation of zero is true. alert(!undefined); // true........ The negation of undefined is true. alert(true === !!"0"); // true........ Double negation of a non–empty String is both true and Boolean. alert("" ? "T" : "F"); // "F"......... The empty String is false. alert(Boolean("false"));// true........ A non–empty String is true. alert(typeof Boolean());// "boolean"... A missing parameter is undefined and thus false. The new operator can be used to create an object wrapper for a Boolean primitive. However, the typeof operator does not return "boolean" for the object wrapper, it returns "object". Because all objects evaluate as true, a method such as .valueOf(), or .toString(), must be used to retrieve the wrapped value. For explicit coercion to the Boolean type, Mozilla recommends that the Boolean() function (without new) be used in preference to the Boolean object. var b = new Boolean(false); // Object false {} var t = Boolean(b); // Boolean true var f = Boolean(b.valueOf()); // Boolean false var n = new Boolean(b); // Not recommended n = new Boolean(b.valueOf()); // Preferred if ( 0 || -0 || "" || null || undefined || b.valueOf() || !new Boolean() || !t ) { alert("Never this"); } else if ( [] && {} && b && typeof b === "object" && b.toString() === "false" ) { } alert("Always this");
JavaScript syntax 24 Native Objects The JavaScript language provides a handful of native objects. JavaScript native objects are considered part of the JavaScript specification. JavaScript environment notwithstanding, this set of objects should always be available. Array An Array is a native JavaScript type specifically designed to store data values indexed by integer keys. Arrays, unlike the basic Object type, are prototyped with methods and properties to aid the programmer in routine tasks (e.g., join, slice, and push). As in the C family, arrays use a zero-based indexing scheme: A value that is inserted into an empty array by means of the push method occupies the 0th index of the array. var myArray = []; // Point the variable myArray to a newly ... // ... created, empty Array myArray.push("hello world"); // Fill the next empty index, in this case 0 alert(myArray[0]); // Equivalent to alert("hello world"); Arrays have a length property that is guaranteed to always be larger than the largest integer index used in the array. It is automatically updated if one creates a property with an even larger index. Writing a smaller number to the length property will remove larger indices. Elements of Arrays may be accessed using normal object property access notation: myArray[1]; //this gives you the 2nd item in myArray myArray["1"]; The above two are equivalent. It's not possible to use the "dot"-notation or strings with alternative representations of the number: myArray.1; // syntax error myArray["01"]; // not the same as myArray[1] Declaration of an array can use either an Array literal or the Array constructor: myArray = [0,1,,,4,5]; // array with length 6 and 6 elements, ... // ... including 2 undefined elements myArray = new Array(0,1,2,3,4,5); // array with length 6 and 6 elements myArray = new Array(365); // an empty array with length 365 Arrays are implemented so that only the elements defined use memory; they are "sparse arrays". Setting myArray[10] = 'someThing' and myArray[57] = 'somethingOther' only uses space for these two elements, just like any other object. The length of the array will still be reported as 58. You can use the object declaration literal to create objects that behave much like associative arrays in other languages: dog = {"color":"brown", "size":"large"}; dog["color"]; // this gives you "brown" dog.color; // this also gives you "brown"
- Page 1 and 2: Essentials of Javascript Visit the
- Page 3 and 4: John Resig 122 Reverse Ajax 123 Ric
- Page 5 and 6: JavaScript 2 History Anyway I know
- Page 7 and 8: JavaScript 4 Prototype-based protot
- Page 9 and 10: JavaScript 6 LCMCalculator.prototyp
- Page 11 and 12: JavaScript 8 Example - use in web p
- Page 13 and 14: JavaScript 10 request that might ha
- Page 15 and 16: JavaScript 12 Application platform
- Page 17 and 18: JavaScript 14 See also • Client-s
- Page 19 and 20: JavaScript 16 [25] Robert Nyman, Ge
- Page 21 and 22: JavaScript syntax 18 JavaScript syn
- Page 23 and 24: JavaScript syntax 20 Primitive data
- Page 25: JavaScript syntax 22 String A Strin
- Page 29 and 30: JavaScript syntax 26 } Math The Mat
- Page 31 and 32: JavaScript syntax 28 // ? - 0 or 1
- Page 33 and 34: JavaScript syntax 30 Operators The
- Page 35 and 36: JavaScript syntax 32 alert( Boolean
- Page 37 and 38: JavaScript syntax 34 Conditional op
- Page 39 and 40: JavaScript syntax 36 With The with
- Page 41 and 42: JavaScript syntax 38 Methods A meth
- Page 43 and 44: JavaScript syntax 40 Inheritance Ja
- Page 45 and 46: JavaScript syntax 42 See also • C
- Page 47 and 48: JavaScript Style Sheets 44 } fontSi
- Page 49 and 50: JavaScript engine 46 JavaScript eng
- Page 51 and 52: Ajax (programming) 48 Ajax (program
- Page 53 and 54: Ajax (programming) 50 • Ajax-powe
- Page 55 and 56: ?: 52 ?: In computer programming, ?
- Page 57 and 58: ?: 54 Furthermore, the ternary oper
- Page 59 and 60: ?: 56 ?: in style guidelines Some c
- Page 61 and 62: Appcelerator Titanium 58 See also
- Page 63 and 64: Bookmarklet 60 Bookmarklet A bookma
- Page 65 and 66: Bookmarklet 62 References [1] Domai
- Page 67 and 68: Client-side JavaScript 64 implement
- Page 69 and 70: Client-side JavaScript 66 Reference
- Page 71 and 72: dhtmlx [1] Comparison of JavaScript
- Page 73 and 74: ility / ion Comparison of JavaScrip
- Page 75 and 76: Comparison of JavaScript frameworks
JavaScript syntax 23<br />
Boolean<br />
JavaScript provides a Boolean data type with true and false literals. The type<strong>of</strong> operator returns the string "boolean"<br />
for these primitives. In a logical context, automatic type coercion evaluates 0, -0, null, NaN, undefined or the empty<br />
string ("") as false. Everything else evaluates as true, including the strings "0", "false" and all objects. Automatic<br />
coercion can sometimes be avoided by using the type checked comparison operators, (=== and !==).<br />
// Automatic type coercion<br />
alert(true == 1); // true.... Only ±0 and NaN are false Numbers.<br />
alert(true == "0"); // true.... Only the empty String is false.<br />
// Type checked comparison<br />
alert(true === 1); // false... The data types don't match.<br />
alert(true === "0"); // false... The data types don't match.<br />
The binary logical operators returned a Boolean value in early versions <strong>of</strong> JavaScript, but now they return one <strong>of</strong> the<br />
operands instead. The left–operand is returned if it can be evaluated as: false, in the case <strong>of</strong> conjunction (a && b), or<br />
true, in the case <strong>of</strong> disjunction (a || b); otherwise the right–operand is returned. An expression can be explicitly cast<br />
to a Boolean primitive by: doubling the logical negation operator (!!), using the Boolean() function, or using the<br />
conditional operator (c ? t : f).<br />
alert({} || null); // Object {}... Any Object is true.<br />
alert(!0 && null); // null........ The negation <strong>of</strong> zero is true.<br />
alert(!undefined); // true........ The negation <strong>of</strong> undefined is<br />
true.<br />
alert(true === !!"0"); // true........ Double negation <strong>of</strong> a non–empty<br />
String is both true and Boolean.<br />
alert("" ? "T" : "F"); // "F"......... The empty String is false.<br />
alert(Boolean("false"));// true........ A non–empty String is true.<br />
alert(type<strong>of</strong> Boolean());// "boolean"... A missing parameter is<br />
undefined and thus false.<br />
The new operator can be used to create an object wrapper for a Boolean primitive. However, the type<strong>of</strong> operator<br />
does not return "boolean" for the object wrapper, it returns "object". Because all objects evaluate as true, a method<br />
such as .valueOf(), or .toString(), must be used to retrieve the wrapped value. For explicit coercion to the Boolean<br />
type, Mozilla recommends that the Boolean() function (without new) be used in preference to the Boolean object.<br />
var b = new Boolean(false); // Object false {}<br />
var t = Boolean(b); // Boolean true var<br />
f = Boolean(b.valueOf()); // Boolean false var n<br />
= new Boolean(b); // Not recommended<br />
n = new Boolean(b.valueOf()); // Preferred<br />
if ( 0 || -0 || "" || null || undefined || b.valueOf() || !new<br />
Boolean() || !t ) {<br />
alert("Never this");<br />
} else if ( [] && {} && b && type<strong>of</strong> b === "object" && b.toString() ===<br />
"false" ) {<br />
}<br />
alert("Always this");