Essentials of Javascript - Cultural View

Essentials of Javascript - Cultural View Essentials of Javascript - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

JavaScript syntax 31 Assignment = Assign += Add and assign -= Subtract and assign *= Multiply and assign /= Divide and assign %= Modulus and assign var x = 1; x *= 3; alert( x ); // displays: 3 x /= 3; alert( x ); // displays: 1 x -= 1; alert( x ); // displays: 0 Comparison == Equal != Not equal > Greater than >= Greater than or equal to < Less than

JavaScript syntax 32 alert( Boolean( -2 ) === true ); // All objects return true alert( Boolean( this ) === true ); alert( Boolean( {} ) === true ); alert( Boolean( [] ) === true ); // These types return false alert( Boolean( null ) === false ); alert( Boolean( undefined ) === false ); // equivalent to Boolean() The NOT operator evaluates its operand in as a Boolean, and returns the negation. Using the operator twice in a row, as a double negative, explicitly converts an expression to a primitive of type Boolean: alert( !0 === Boolean( !0 ) === !!1 === Boolean( 1 ) === true ); alert( !!0 === Boolean( 0 ) === !1 === Boolean( !1 ) === false ); alert( !"" === Boolean( !"" ) === !!"s" === Boolean( "s" ) === true ); alert( !!"" === Boolean( "" ) === !"s" === Boolean( !"s" ) === false ); Expressions that use features such as post–incrementation, (i++), have an anticipated side effect. JavaScript provides short-circuit evaluation of expressions; the right operand is only executed if the left operand does not suffice to determine the value of the expression. alert( a || b ); // When a is true, there is no reason to evaluate b. alert( a && b ); // When a is false, there is no reason to evaluate b. alert( c ? t : f ); // When c is true, there is no reason to evaluate f. In early versions of JavaScript and JScript, the binary logical operators returned a Boolean value (like most C–derived programming languages). However, all contemporary implementations return one of their operands instead: alert( a || b ); // if a is true, return a, otherwise return b alert( a && b ); // if a is false, return a, otherwise return b Programmers who are more familiar with the behavior in C might find this feature surprising but it allows for a more concise expression of patterns like null coalescing: var s = t || "(default)"; // assigns t, or the default value if t is null, empty, etc.

JavaScript syntax 32<br />

alert( Boolean( -2 ) === true );<br />

// All objects return true<br />

alert( Boolean( this ) === true );<br />

alert( Boolean( {} ) === true );<br />

alert( Boolean( [] ) === true );<br />

// These types return false<br />

alert( Boolean( null ) === false );<br />

alert( Boolean( undefined ) === false ); // equivalent to Boolean()<br />

The NOT operator evaluates its operand in as a Boolean, and returns the negation. Using the operator twice in a row,<br />

as a double negative, explicitly converts an expression to a primitive <strong>of</strong> type Boolean:<br />

alert( !0 === Boolean( !0 ) === !!1 === Boolean( 1 ) === true<br />

);<br />

alert( !!0 === Boolean( 0 ) === !1 === Boolean( !1 ) === false<br />

);<br />

alert( !"" === Boolean( !"" ) === !!"s" === Boolean( "s" ) === true<br />

);<br />

alert( !!"" === Boolean( "" ) === !"s" === Boolean( !"s" ) === false<br />

);<br />

Expressions that use features such as post–incrementation, (i++), have an anticipated side effect. JavaScript provides<br />

short-circuit evaluation <strong>of</strong> expressions; the right operand is only executed if the left operand does not suffice to<br />

determine the value <strong>of</strong> the expression.<br />

alert( a || b ); // When a is true, there is no reason to evaluate<br />

b.<br />

alert( a && b ); // When a is false, there is no reason to evaluate<br />

b.<br />

alert( c ? t : f ); // When c is true, there is no reason to evaluate<br />

f.<br />

In early versions <strong>of</strong> JavaScript and JScript, the binary logical operators returned a Boolean value (like most<br />

C–derived programming languages). However, all contemporary implementations return one <strong>of</strong> their operands<br />

instead:<br />

alert( a || b ); // if a is true, return a, otherwise return b<br />

alert( a && b ); // if a is false, return a, otherwise return b<br />

Programmers who are more familiar with the behavior in C might find this feature surprising but it allows for a more<br />

concise expression <strong>of</strong> patterns like null coalescing:<br />

var s = t || "(default)"; // assigns t, or the default value if t is<br />

null, empty, etc.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!