8. Operators in Javascript

Operators
Operators are the special symbols that is used to perform some operation on oprends in an exporession
Airthmatic operator
used for basic mathematical operations.
Examples: + , - , , / , % , **
Assignment operators
used to assign values to variables.
Examples: = , += , -= , *= , /=
Comparison operators
return true or false.
Examples: == , === , !== , > , < , >= , <=
Logical operators
Logical operators are used for boolean operations.
Examples: && , || , !
Unary Operators
Unary operators operate on a single operand.
Examples: + , - , ++ , - -
Bitwise operators
Bitwise operators perform operations on binary numbers.
Examples: & , | , ^ , ~ , « , »
Conditional (Ternary ) Operator
Shorthand for
if-else
.condition ? trueValue : falseValue;
Comma operator
Allows multiple expressions to be evaluated in a single statement.
let x = (1, 2, 3); console.log(x); // 3 (last expression is returned)
BigInt Operator
BigInt (
n
suffix) is used for large numbers.let big = 9007199254740991n + 1n; console.log(big); // 9007199254740992n
String Operator
The
+
operator is used for concatenation.let name = "Hello" + " World!"; console.log(name); // "Hello World!"
Interview Questions
What will be the output?
console.log("5" + 2); // 52 console.log("5" - 2); //3 console.log(null == undefined); // true console.log(null === undefined); // false
"5" + 2
→"52"
(concatenation) Implicit(Type Coercion) type conversion in JS"5" - 2
→3
(numeric conversion)
What is the result of
true + true
?true
is converted to1
, so1 + 1 = 2
.What will be the output ?
console.log(false ? "Truthy" : "Falsy"); // falsy console.log("" ? "Truthy" : "Falsy"); // falsy console.log(0 ? "Truthy" : "Falsy");// falsy console.log(-0 ? "Truthy" : "Falsy");// falsy console.log(null ? "Truthy" : "Falsy");// falsy console.log(NaN ? "Truthy" : "Falsy");// falsy console.log(undefined ? "Truthy" : "Falsy");// falsy console.log({} ? "Truthy" : "Falsy"); // Truthy console.log([] ? "Truthy" : "Falsy"); // Truthy console.log(a ? "Truthy" : "Falsy"); // Truthy
let a = 1; let b = (a++, a); // comma operator console.log(b);
Output :
2
(a++, a)
executesa++
, but returns the last expression (a
).console.log({} == {}); // false console.log([] == []); // false console.log([] == ![]); // true
Objects (
{}
or[]
) are compared by reference, not value.
Next Topic
Subscribe to my newsletter
Read articles from Ayush Rajput directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
