crud ajax php mysql

WHAT IS TERNARY OPERATOR IN JAVASCRIPT ?

It is used for decision making.

For ternary operator,JavaScript has got inspired from C language.

Ternary operator is the only operator which has three operands.

Ternary operator is an expression and return a value.It cannot be used to execute code.

It must be either returned in a method, or set equal to a variable with the same data type as the returned values.

It is used as an best alternative of "IfElse statement".

It is also known as conditional operator.

It is often used to shorten the code.That too can be written in single line.

Some times it makes hard to read the code.

SHOULD WE USE TERNARY OPERATOR ?.

Personally,It should be used if your code is not complicated.

If it is used correctly than it can reduce the amount of lines of code and readability.

In real time example,Normally ternary operator is used for assigning post data or validating form.

WORKING OF TERNARY OPERATOR

ternary block

WORKING OF IF ELSE STATEMENT

ternary block

SYNTAX

CONDITION ? true value : false value

FIRST ARGUMENT- It takes the condition statement.It returns either true or false

SECOND ARGUMENT- It gets executed whenever condition gets truth value.

THIRD ARGUMENT- It gets executed whenever condition gets false value.

The "?" separator divides the conditional statement to the true value.

The ":" separotor divides the true value to the false value

BASIC EXAMPLE


       <script>
          var isStatement='true';
          var result= isStatement ? 'success' :'failure';
          document.write(result);
       </script>
        

The variable 'isStatement' holds the boolean value that is 'true'.

When the first operand of the ternary operator returns true then it executes second operand.

When the first operand of the ternary operator returns false then it executes third operand.

The output of the above code is "true".

SIMPLE EXAMPLE

If Else Statement


       <script>
          var abc=10;
          if((abc%2)==0)
          {
            var xyz="even";
          }
          else
          {
            var xyz="odd";
          }
          document.write(xyz);
       </script>
        

A variable "abc" is declared and initialized to value 10.

The if condition checks whether the remainder is equal to zero or not.

Displays "even" if the condition satisfies or displays "odd".

The above code can be converted into single line of code by using ternary operator.

Ternary Operator


      <script>
         var abc=10;
         var xyz=((abc%2)==0)? 'even':'odd';
         document.write(xyz);
      </script>
       

In the above code variable "abc" holds value 10.

The second line of the code contains the ternary operator.

The first operand of the ternary operator contains the condition which checks whether the remainder is equal to zero.

If the condition statement returns true then second operand gets executed.Displays "even".

If the condition statement returns false then third operand gets executed.Displays "odd".

SIMPLE EXAMPLE OF JAVASCRIPT OBJECT

If Else Statement


     <script>
     let emp = {
       name: 'jack',
       salary: 20000,
       statement: null
     };
     if(emp.salary>=5000)
     {
       statement="true";
     }
     else
     {
       statement="false";
     }
     document.write(emp.statement);
     </script>
      

An object "emp" is created.Few properties are loaded into it.

Dot operator is used to access the variable "name".

When "If condition" satisfies then variable "statement" holds the "true" boolean expression.

When "If Condition" fails then variable "statement" holds the "false" value.

the output for the above code is "true".

The above if else code can be converted into single line of code by using ternary operator.

Ternary Operator


    <script>
    let emp = {
      name: 'jack',
      salary: 200,
      statement: null
    };
    emp.statement = emp.salary >=5000 ? 'true' : 'false';
    document.write(emp.statement);
    </script>
     

In the above code, javascript object "emp" is created with its few properties("name","salary","statement").

The first property is "name" which holds "jack".

The second property is "salary" which holds "20000".

The third property is "statement" which holds "NULL".

The first operand of the ternary operator holds the condition which checks whether salary is greater than or equal to 5000.

If the first operand truns true then second operand gets executed.

If the first operand truns false then third operand gets executed.

the output for the above code is "false".

EXAMPLE OF NESTED TERNARY OPERATOR

If Else Statement


   <script>
   var num1 = 100;

   if(typeof num1=='number')
   {
     if((num1%2)==0)
     {
       document.write("number is even");
     }
     else
     {
        document.write("number is odd");
     }
   }
   else
   {
     document.write("not a number");
   }
   </script>
    

The above if else code can be converted into single line of code by using ternary operator.

In the above example, 'num1' variable is declared of type int.

It holds the value "100".

"typeof" it is the operator which is used to identify the datatype of the operand.

The outer if statement holds the condition whether the 'num1' variable is a number data type or some other type.

The outer else statement executes as "not a number".

The inner if statement holds the condition whether the 'num1' variable is of even type or odd type.

Ternary Operator


  <script>
  var num1=100;
  var result=(typeof num1=='number')? ((num1%2)==0) ? "even" : "odd" : "not a number";
  document.write(result);

  </script>
   

In the above code 'num1' variable is declared and initialized with 100.

if the first condition gets true then the control goes to the second condition or else it displays "not a number".

If the second condition goes true then it displays "even" ,if it goes false then it displays "odd".

CONCLUSION

In this tutorial we have learnt the working of ternary operator of javascript, converting simple if else statement to ternary operator and converting nested if else statement to ternary operator.

I hope you have liked the article.Please E-mail me your feedback and thank you for following the article.

About author
Supreet Patil
I am a web developer.I have started the journey of web development 2017.I am CSE degree holder.
E-mail:psupreet12@gmail.com
Leave a reply