Sunday, July 22, 2018

Lear Typescript Ternary, String and Negation Operators

Conditional Operators. 

 This are also called ternary operator. This operators are conditional logic in which condition is evaluated and returns the conditional logic.

Syntax Condition? true-result? false-result;

The condition is an expression which is evaluated first. if the condition is true, true-result is evaluated if the condition is false, false-result is evaluated
We will see the example

Even Number check using the ternary operator

var number1 = 15;
var number2= 16;
console.log(number1 %2  ? "Even Number" : "Odd Number"); // returns Even Number
console.log(number2 %2  ? "Even Number" : "Odd Number"); // returns Odd Number

String Concatenation Operator 

This is also called string append operators which are operated on strings and symbol is + (plus). Appends the strings from the right side and assign to the left side. It does not add any extra space as part of string concat operations

var appendTest = "Concat"+" values" 
console.log(appendTest) // returns Concat values
plus operator can be applied to multiple strings and append the strings and returns the concatenated string

Negation Operator 

This operator changes the sign of a number and symbol is -(minus) if the value is positive, negation returns negative if the value is negative negation returns positive
var number1 = 8;
console.log(number1);   //returns 8
console.log(-number1);   // returns -8

TypeOf and InstaneOf operator 

both operators are unary operators. So these operators have only single operand typeOf operator returns data type of the value instaceOf operator checks if the specified object is of the generic object type or not.  Please see this Complete typeOf,Instanceof examples post for more examples.
var strinTest ='test' 
console.log(typeof strinTest);   //returns : string