Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

jobs.webdeveloper.com

e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


The Conditional Operator

Simply put, the conditional operator is the following: "( (expression) ? trueValue : falseValue )". This operator fulfills the same functionality as an if / else statement, but in a kind of shorthanded form. An expression that evaluates to a Boolean (true or false) must be placed to the left of the question mark. If the expression evaluates to true, the first value after the question mark is returned from the operation. If the expression evaluates to false, the value after the colon is returned from the operation. This shorthanded if / else statement is especially useful to decrease the amount of typing you'll have to do. It seems to make more sense to me than does the if / else statement. The following example, taken from the JavaScript Language Reference, shows the use of both the less-than operator and the conditional operator. It is a simple example that shows the use of the conditional operator very well.

var n = new String("67");
if ((n < 100) ? document.write("The result is true") 
: document.write("The result is false")); 

The example shows the use of the conditional and less-than operators nicely. First the variable "n" is created, which holds the number 67, which is of the string data type. Then an if statement is started with the conditional operator as the condition. The "n<100", which is read as "if n is less than 100", obviously compares to true since the value of n is 67, which is less than 100. This comparison tells the browser to display the "true" result, so the string "The result is true" will be written to the screen. If the result was false, the string "The result is false" would be written to the screen. So much for the conditional operator.

Bitwise Operators

You might notice in our exploration of the bitwise operators that they look very much like the various comparison operators. But in fact they differ hugely in their functionality. A operator is said to be "bitwise" when its values are the direct result of a binary computation of some sort. The given integer is converted to a 32 bit binary number, and then something is done to it. They are indispensable when working with binary numbers (decimal equivalents of binary numbers). If the integer used to represent the binary integer doesn't evaluate to 32 bits long, JavaScript automatically adds on the required amount of zeros to achieve the required 32 bit length.
  • & - Bitwise AND - The bitwise AND operation looks at the value to the left of the operator and the value on the right of the operator as 32 bit binary numbers. These binary ones and zeros are then ANDed and the result is returned as a decimal integer from the operation.
  • | - Bitwise OR - The bitwise OR operation looks at the value to the left of the operator and the value on the right of the operator as 32 bit binary numbers. These binary ones and zeros are then ORed and the result is returned as a decimal integer from the operation.
  • ^ - Bitwise Exclusive OR (XOR) - The bitwise XOR operation looks at the value to the left of the operator and the value on the right of the operator as 32 bit binary numbers. These binary ones and zeros are then XORed and the result is returned as a decimal integer from the operation.
  • ~ - Bitwise NOT - The NOT operand is basically an inverter. It changes all of the ones in the 32 bit binary equivalent to zeros and all of the zeros to ones. This new binary number is then converted to a decimal integer and returned as the result of the operation.
  • << - Shift Left - The shift left operator is used to shift the binary equivalent of the number to the left of the operator. The number of places to be shifted is given as an integer to the right of the operator. This shifting operation leaves empty places on the right of the 32-bit number. Since the number must always be 32 bits long, zeros are added to the binary number.
  • >> - Shift Right with Sign - The shift right with sign operator is used to shift the binary equivalent of the number to the left of the operator. The number of places to be shifted is given as an integer to the right of the operator. This shifting operation leaves empty places on the left of the 32-bit number. Since the number must always be 32 bits long, numbers must be added. If the original number is positive, ones are added to make the number 32 bits long again. If the number was originally negative, zeros are added.
  • >>> - Shift Right Zero Fill - The shift right zero fill operator is used to shift the binary equivalent of the number to the left of the operator. The number of places to be shifted is given as an integer to the right of the operator. This shifting operation leaves empty places on the left of the 32-bit number. Since the number must always be 32 bits long, numbers must be added. Regardless of the polarity of the original number, zeros are added to make the number once again 32 bits in length.

    We hope you enjoyed this bit on Operators. Check out WDVL next week for more of the JavaScript Chronicles where we'll take a look at conditional statements

    The Logical Operator
    The JavaScript Chronicles
    Conditional Statements

    The JavaScript Chronicles

    JavaScript Introduction
    Part 2: Data Types
    Part 3: Arrays
    Part 4: Operators
    Part 5: Conditional Statements
    Part 6: JavaScript Functions
    Part 7: Pattern Matching - The RegExp Object
    Part 8: Introduction to Server Side JavaScript
    Part 9: Server Side JavaScript Mail Sending
    Part 10: Server Side JavaScript and File Manipulation
    Part 11: Working with Forms in JavaScript Part 12: Getting to Know Dynamic HTML


Up to => Home / Authoring / Tutorials




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers