JavaScript operators are symbols or words that are used to perform operations on values. These operations include mathematical calculations, assignment, comparison, and logical operations. Some common JavaScript operators include:
Arithmetic operators :
These operators perform mathematical calculations on numerical values. Examples include +
(addition), ** (subtraction), **
(multiplication), /
(division), %
(modulus) and ++
(increment).
let x = 5;
let y = 2;
let result = x + y; // 7
Assignment operators :
These operators assign a value to a variable. The most common assignment operator is =
. Other examples include +=
, -=
, *=
, /=
, and %=
.
let x = 5;
x += 2; // x is now 7
Comparison operators :
These operators compare two values and return a Boolean value (true
or false
). Examples include ==
(equal to), !=
(not equal to), >
(greater than), <
(less than), >=
(greater than or equal to), and <=
(less than or equal to).
let x = 5;
let y = 2;
let result = x > y; // true
Logical operators :
These operators are used to combine multiple conditions and return a Boolean value. Examples include &&
(and), ||
(or), and !
(not).
let x = 5;
let y = 2;
let result = x > 2 && y < 10; // true
ternary operator :
It is a shorthand way of writing an if-else
statement. It takes three operands, a condition, a statement to execute if the condition is true and a statement to execute if the condition is false.
let x = 5;
let y = 2;
let result = x > 2 ? 'Greater' : 'Lesser'; // Greater
typeof operator :
It is used to check the data type of a value. It returns a string indicating the type of the operand.
let x = 5;
let result = typeof x; // number
These are just some of the most commonly used operators in JavaScript. By using these operators, you can perform various operations and calculations in your JavaScript code.
JOIN WHATSAPP COMMUNITY FOR MORE SUCH AWESOME CONTENT :
<aside> 💡 CLICK HERE TO JOIN
</aside>
Join Instagram Community ( 80k+) : @webdevarmy & @cssdevarmy
Youtube : @webdevarmy