JavaScript Operators
- arithmetic operators( + - * / % ++ -- ) :
- Example: (5+6)*10
----------------------------------
- assignment operators ( = ). . (=) is an "assignment" operator, not an "equal to" operator.:
- Example: var x;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). Example: x<8; returns true on execution.
- > (greaterthan). Example: x>8; returns false on execution.
- <= (lessthan or equal to). Example: x<=8; returns true on execution.
- >=(greaterthan or equal to). Example: x>=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( ? : ).
- Example: var voteable = (age < 18) ? "Too young":"Old enough";
----------------------------------
- Example: (5+6)*10
- Example: var x;x = 6;. The "equal to" operator is written like == in JavaScript. Other assignment operators are : (+= -= *= /= %=)
- == (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). Example: x<8; returns true on execution.
- > (greaterthan). Example: x>8; returns false on execution.
- <= (lessthan or equal to). Example: x<=8; returns true on execution.
- >=(greaterthan or equal to). Example: x>=8; returns false on execution.
if (isNaN(age)) {
voteable = "Error in input";
} else {
voteable = (age < 18) ? "Too young" : "Old enough";
}
----------------------------------
- (x < 10 && y > 1) is true
- (x == 5 || y == 5) is false
- !(x == y) is true
- Example: var 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)
----------------------------------
- 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
- link to bitwise operations
----------------------------------
- link to bitwise operations

No comments:
Post a Comment