Operators
- In JavaScript you have different kinds of operators.
- You use these operators along with our variables and constants to create expressions and with these expressions you can implement logic and algorithms.
- In JavaScript you have arithmetic operators, assignment operators, comparison operators, logical operators, and bitwise operators
Arithmetic Operators
- Arithmetic operators are used for performing calculations, just like the calculations in mathematics
- Arithmetic operators usually take two operands and then produce a new value.
x + y
referred as an expression in JavaScript. An expression produces a value.- Addition
+
, Subtraction-
, Multiplication*
, Division/
, Division Remainder/Modulus%
, Exponential**
, Incremental++
, Decremental--
let x = 5;
let y = 2;
console.log(x + y);
console.log(x - y);
console.log(x * y);
console.log(x / y);
console.log(x % y);
console.log(x ** y);
Modulus (%)
- Division of two integers produces a quotient and a remainder.
- Result of a modulo operation is the remainder of an arithmetic division.
Increment Operators (++)
- Based on the position operator will behave differently
- If you put the increment operator before x (prefix/pre increment) the value of x will be incremented by 1 first and then you will see that on the console
- If you put this operator after x (posffix/post increment), you will see x on the console first, and then the value of x will be incremented by 1
Pre Increment/Prefix
let x = 5;
let z = ++x;
console.log(z);
console.log(x);
Post Increment/Postfix
let x = 5;
let z = x++;
console.log(z);
console.log(x);
Decrement Operators (--)
- Based on the position operator will behave differently
- If you put the decrement operator before x (prefix/pre increment) the value of x will be decremented by 1 first and then you will see that on the console
- If you put this operator after x (posffix/post increment), you will see x on the console first, and then the value of x will be decremented by 1
Pre Decrement/Prefix
let x = 5;
let z = --x;
console.log(z);
console.log(x);
Post Decrement/Postfix
let x = 5;
let z = x--;
console.log(z);
console.log(x);
Assignment Operators
Assignment Operator
let x = 5
Addition Assignment Operator
x += 2;
equivalent to
x = x + 2
Subtraction Assignment Operator
x -= 2;
equivalent to
x = x - 2
Multiplication Assignment Operator
x *= 2;
equivalent to
x = x * 2
Division Assignment Operator
x /= 2;
equivalent to
x = x / 2
Remainder Assignment Operator
x %= 2;
equivalent to
x = x % 2
Expontential Assignment Operator
x **= 2;
equivalent to
x = x ** 2
Comparison Operators
- You use these operators to compare the value of a variable with some thing else.
- The result of an expression, that includes a comparison operator is a boolean, it's true or false
Relational operators
>
greater than
>=
greater than equal to
<
less than
<=
less than equal to
Equality operators
Strict equality operator ===
- Strict equality operator ensures that both operands are of the same type and of the same value.
console.log(1 === 1); //true
console.log(1 === "1"); //false
console.log(1 == true); //false
Loose equality operator ==
- It will only check if the values are equal.
- If the types don't match, it will convert the type of what you have on the right side to match what you have on the left side and then checks value
console.log(1 == 1); //true
console.log(1 == "1"); //true
console.log(1 == true); //true
Not Equal Operator
!==
- not equal value or not equal type
!=
- not equal value
Ternary Operator or Conditional Operator
- Assigns a value to a variable based on some condition.
let marks = 80;
let grade = marks > 90 ? "A": "B";
console.log(grade);
Logical Operators
&&
(and)
||
(or)
!
(not)
- Javascript engine evaluates an expression from left to right
Short circuiting
- The
&&
operator will return false as soon as it gets any false value and will return true if all the values are true. - The
||
operator will return the true as soon as it gets any true value and will return false if all the values are false
Falsy/Falsey
- Everything without a value is
Falsy
- A
Falsy/Falsey
value is a value that is consideredfalse
when encountered in a Boolean context. - Falsy values are
- undefined
- null
- 0, -0
- false
- "" (empty string)
- NaN (not a number)
Truthy
- Everything with a value is
Truthy
- A truthy value is a value that is considered true when encountered in a Boolean context.
- Truthy values
- true
- , []
- 40, -42
- 6.3, -6.3
- "0"
- "false"
Logical Opearators with Non-Boolean Values
- The result of a logical expression is not necessarily a true or false, it depends on the value of the operands you have
- When JavaScript engine tries to evaluate an expression, it looks at each operand if that operand is not a boolean true or false, it will try to interpret it as
truthy
orfalsey
,
&&
Operator
- The
&&
operator will returnfalsy
value as soon as it gets anyfalsy
value and will return the last value if all other values aretruthy
.
console.log(true && null); //null
console.log(true && undefined); //undefined
console.log(true && 'Ganesh'); //Ganesh
console.log(true && 1); //1
console.log(true && null); //null
console.log(true && undefined); //undefined
||
Operator
- The
||
operator will returntruthy
value as soon as it gets anytruthy
value and will return the last value if all other values arefalsy
.
console.log('Ganesh' || true); //Ganesh
console.log(1 || true); //1
console.log(false || null); //null
console.log(false || undefined); //undefined