Wednesday, December 21, 2016

Javascript-5-Javascript as a language II -Operators / isNAN() / Comparison


JavaScript Operators


  • arithmetic operators( + - *  / % ++ -- ) 
    • Example(5+6)*10
----------------------------------
  • assignment operators ( = ). . (=) is an "assignment" operator, not an "equal to" operator.: 
    • Examplevar x;= 6;. The "equal to" operator is written like == in JavaScript. Other assignment operators are : (+=  -=  *=  /=  %=)
----------------------------------

Note:
Comparison and Logical operators are used to test for true or false.
  • Comparison operators:  used in logical statements to determine equality or difference between variables or values. Lets say, x=5
    • == (equals to value).  
      • x==8; returns false on execution.
      • x==5; returns true on execution.
      • x=="5"; returns true on execution.<===
    • === (equals to value and equal to type).
      • x===5; returns true on execution.
      • x==="5"; returns false on execution.<===
    • != (not equals to value).
      • x!=8; returns true on execution.
    • !== (not equals value or type).
      • x!==5; returns false on execution.
      • x!=="5"; returns true on execution.
      • x!==8; returns true on execution.
    • (lessthan). Examplex<8; returns true on execution.
    • (greaterthan). Examplex>8; returns false on execution.
    • <= (lessthan or equal to). Examplex<=8; returns true on execution.
    • >=(greaterthan or equal to). Examplex>=8; returns false on execution.
More Examples:
Notes: 
* When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. 
* An empty string converts to 0
* A non-numeric string converts to NaN which is always false.
* When comparing two strings, "2" will be greater than "12", since (alphabetically) 1 is less than 2.
* when an integer is compared to a NAN(example:  "john" or "diane" ). the expressions always results in false. hence, 2<"John" (or) 2>"John" (or) 2=="John", all returns false.



isNAN() : checks if a given variable is a number
Example:
age = Number(age);
if (isNaN(age)) {
    voteable = "Error in input";
else {
    voteable = (age < 18) ? "Too young" : "Old enough";
}























----------------------------------
  • Logical operators ( &&  ||  ! ). .used to determine the logic between variables or values.: Example: if x = 6 and y = 3
    • (x < 10 && y > 1) is true
    • (x == 5 || y == 5) is false
    • !(x == y) is true

----------------------------------
  • Ternary Operator(  ?  :  ). 
    • Examplevar voteable = (age < 18) ? "Too young":"Old enough";
----------------------------------

  • Unary operator:
    • can be used to convert a variable to a number:
      • var y = "5";// y is a string
      • var x = + y;// x is a number
    • If the variable cannot be converted, it will still become a number, but with the value NaN (Not a number):
      • var y = "John";// y is a string
      • var x = + y;// x is a number (NaN)
----------------------------------

  • Bitwise operators ( = ). . (=) is an "assignment" operator, not an "equal to" operator.: Example

----------------------------------

No comments:

Post a Comment