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 31<br />

Assignment<br />

= Assign<br />

+= Add and assign<br />

-= Subtract and assign<br />

*= Multiply and assign<br />

/= Divide and assign<br />

%= Modulus and assign<br />

var x = 1;<br />

x *= 3;<br />

alert( x ); // displays: 3<br />

x /= 3;<br />

alert( x ); // displays: 1<br />

x -= 1;<br />

alert( x ); // displays: 0<br />

Comparison<br />

== Equal<br />

!= Not equal<br />

> Greater than<br />

>= Greater than or equal to<br />

< Less than<br />

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

Saved successfully!

Ooh no, something went wrong!