Day 3 : What is an Operators and Operands in Javascript ?
learning is a life of lesson
The numbers (in an arithmetic operation) are called operands.
The operation (to be performed between the two operands) is defined by an operator.
Operators:-
There are different types of JavaScript operators:
● Arithmetic Operators
● Assignment Operators
● Comparison Operators
● Logical Operators
● Conditional Operators
● Type Operators
Arithmetic Operators:-
A typical arithmetic operation operates on two numbers.
Operator | Description |
+ | Addition |
- | Subtraction |
| Multiplication |
/ | Division |
% | Modulus (Division Remainder) |
* | Exponentiation (ES2016) |
++ Increment
-- Decrement
Example:-
let x = 10, y = 20;
console.log(x + y); // 30
console.log(x - y); // -10
console.log(x * y); // 200
console.log(y / x); // 2
console.log(y % x); // 0
console.log(5 ** 2); // 25
console.log(a++); // 10
console.log(++a); // 11
console.log(a–-); // 10
console.log(--a); // 9
Assignment Operators:-
Assignment operators assign values to JavaScript variables.
Operator | Example | Same As |
= | x = y | x = y |
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
= | x = y | x = x y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
= | x = y | x = x * y |
Example:-
let x = 10, y = 20;
let z = x; // z = 10
console.log(x += y); // 30
console.log(x -= y); // -10
console.log(x *= y); // 200
console.log(y /= x); // 2
console.log(y %= x); // 0
console.log(y **= x); // 25
Comparison Operators:-
Operator | Description |
== | Equal to |
=== | Equal value and equal type |
!= | Not equal |
!== | Not equal value and not equal type |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
? : | Ternary Operator |
Example:-
console.log(10 == 10); // true
console.log(10 === 10); // true
console.log(“0” == false); // true
console.log(false == “0”); // true
console.log(false === “0”); // false
console.log(10 != 20); // true
console.log(0 ! = false); // true
console.log(10 > 20); // false
console.log(10 >= 20); // false
console.log(10 < 20); // true
console.log(10 < = 20); // true
Type Operators:-
Operator | Description |
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type
Logical Operators:-
Operator | Description |
&& | Logical And |
|| | Logical OR |
! | Logical Not |
^ | Logical XOR |
Example:-
let a = 2, b = 3, c;
c = –-a && b++; // if first condition value is 1 after it is continue console.log(a,b,c); // 1, 4, 3
c = –-a || b++; // 1, 3, 1
console.log(a ! = b); // true
console.log(1 ^ 1); // 0
console.log(0 ^ 1); // 1
console.log(1 ^ 0); // 1
console.log(0 ^ 0); // 0
console.log(1^0^1); // 0
console.log(0^1^0); // 1
console.log(1^1^1); // 1
console.log(0^0^0); // 0
Operator Precedence:-
Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
And (as in school mathematics) the precedence can be changed by using parentheses.
When using parentheses, the operations inside the parentheses are computed first.
Null(Object):-
The null value represents the intent absence of any object value. It is one of javascript’s primitive values and is treated as falsy for boolean operations.
Undefined:-
A variable that has not been assigned a value is of type undefined . Example:-
let a;
console.log(a); // value is undefined, type is undefined
Empty Values:-
An empty value has nothing to do with undefined. An empty string has a legal value and a type.
NaN:- (Not a number)
NaN type of number. Nan is a number that is not a legal number. Example:-
let a;
a = 10*“s” ;
console.log(a); // value is NaN, type is Number
console.log(null == NaN); // false
console.log(null === NaN); // false
console.log(null == undefined); // true
console.log(null === undefined); // false
console.log(undefined == NaN); // false
console.log(undefined === NaN); // false
console.log(true == undefined); // false console.log(false == undefined); // false
console.log(true == NaN); // false console.log(false == NaN); // false
console.log(true == null); // false console.log(false == null); // false