----: Expression And Operators in JavaScript :----
1. Assignment Operators (=)
An assignment Operator assign a value to its left operand based on the value
of its right operand
The simple assignment operator is equal (=)
var x = 5;
var y = 5;
2. Arithemetic Operators
An Arithemetic Operators takes numerical Values (either literals or variables)
as their operands and return a single numberical value.
console.log(3+4);
console.log(2-5);
console.log(40*5);
console.log(40/8);
console.log("Remainder operator : " + 35%2)
Increment & Decrement operator :
Operator: x++ or ++x or x-- or --x
If used postfix with operator after operand (for example x++),
The Increment operator increments and return the value before increasing.
var num = 15;
var newNum = num++;
console.log(num);
console.log(newNum);
If used prefix with operator before operand (for example ++x),
then the expression is evaluate using the new value of the variable.
var num = 15;
var newNum = ++num;
console.log(num);
console.log(newNum);
3. Comparison Operators
A comparision Operator compares its operand and
returns a logical value based on whether the comparison is true or false
var a = 30;
var b = 10;
Equal (==)
console.log(a==b);
Not equal (!=)
console.log(a!=b);
Greater than (>)
console.log(a>b);
Greater than or equal (>=)
console.log(a>=b);
Less than Operator (<)
console.log(a<b);
Less than or equal (<=)
console.log(a<=b);
4. Logical Operators
Logical operators are typical used with boolean (logical) Values;
when they are , they return a Boolean value.
var a = 30;
var b = -20;
Logical AND (&&)
The Logical AND (&&) operator (logical conjuction) for a set of
Operand is true if and only if all of its operand are true.
All expression need to be true otherwise it returns false
var a = 30;
var b = -20;
console.log(a>b && b<0 && a >0);
Logical OR (||)
The Logical OR (||) operator (logical disjunction) for a set of
operand is true if and only if one or more of its operand is true.
if only one expression is true it retruns true
var a = 30;
var b = -20;
console.log((a>b) || (b>a) || (a=b));
Logical NOT (!)
The Logical NOT (!) operator (logical complement , negation)
takes truth to falsity and vice versa.
It changes true into false and false into true
var a = 30;
var b = -20;
console.log(!((a>0) || (b<0)));
console.log(!(false));
5. String Concatenation (Operators)
The concatenation operators (+) concatenates two strings values together,
returning another string that is the union of two operand string.
console.log("Hello world");
console.log("Hello " + "world");
6. Conditional Operators
Challenge Time:-
Q1. What will be the output of 3**3?
console.log(3**3); // its like power
Q2. What will be the output when we add a number and a string?
console.log(5 + "Himanshu");
Q3. write a program to swap two numbers?
var a = 5;
var b = 10;
output b=5,a=10
var c = b; //c=10
b = a; // b = 5;
a=c;
console.log("The value of a is " + a);
console.log("The value of b is " + b);
Q4. write a program to swap two numbers using third variables?
var a = 5;
var b = 10;
output b=5,a=10
a = a + b; // a = 15
b = a - b; // b = 5
a = a - b; // a = 10
console.log("The value of a is " + a);
console.log("The value of b is " + b);
Interview Question :-
What is the Difference between == vs ===?
(==)
var num1 = 5;
var num2 = "5";
console.log((typeof(num1)));
console.log((typeof(num2)));
console.log(num1 == num2);
(===)
var num1 = 4;
var num2 = "4";
console.log((typeof(num1)));
console.log((typeof(num2)));
console.log(num1 === num2);
{==} check the value of only where as ,
{===} checks value as well as Data types.