Miscellaneous Operator

We will discuss two operators here that are quite useful in JavaScript: theconditional operator (? πŸ™‚ and the typeof operator.

Conditional Operator (? πŸ™‚

The conditional operator first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.

Sr.No Operator and Description
1 ? : (Conditional )

If Condition is true? Then value X : Otherwise value Y

Example

Try the following code to understand how the Conditional Operator works in JavaScript.

<html>
   <body>
   
      <script type="text/javascript">
         <!--
            var a = 10;
            var b = 20;
            var linebreak = "<br />";
         
            document.write ("((a > b) ? 100 : 200) => ");
            result = (a > b) ? 100 : 200;
            document.write(result);
            document.write(linebreak);
         
            document.write ("((a < b) ? 100 : 200) => ");
            result = (a < b) ? 100 : 200;
            document.write(result);
            document.write(linebreak);
         //-->
      </script>
      
      <p>Set the variables to different values and different operators and then try...</p>
   </body>
</html>

Output

((a > b) ? 100 : 200) => 200 
((a < b) ? 100 : 200) => 100
Set the variables to different values and different operators and then try...

Related Posts

  • 56
    The typeof operator is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand. The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean…
    Tags: operator, result, values, var, javascript
  • 53
    Operators are the constructs which can manipulate the value of operands. Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator. Types of Operator Python language supports the following types of operators. Python Arithmetic Operators Assume variable a holds 10…
    Tags: operators, operator
  • 52
    JavaScript supports the following logical operators βˆ’ Assume variable A holds 10 and variable B holds 20, then βˆ’ Sr.No Operator and Description 1 && (Logical AND) If both the operands are non-zero, then the condition becomes true. Ex: (A && B) is true. 2 || (Logical OR) If any…
    Tags: true, document.write(result, document.write(linebreak, result, operators, operator, var, variables, set, values
  • 46
    Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are calledoperands and β€˜+’ is called the operator. JavaScript supports the following types of operators. Arithmetic Operators Comparision Operators Logical (or Relational) Operators Assignment Operators Conditional (or ternary) Operators Lets have a look…
    Tags: operators, result, document.write(linebreak, document.write(result, var, javascript, operator
  • 42
    Example: !/usr/bin/python a = 21 b = 10 c = 0 c = a + b print "Line 1 - Value of c is ", c c += a print "Line 2 - Value of c is ", c c *= a print "Line 3 - Value of c is…
    Tags: operator, operators

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: