JavaScript Operators: The Basics You Need to Know
In JavaScript, operators are symbols that perform operations on value & varialbes. JavaScript uses various operators to perform mathematical, logical, and comparison tasks on values and variables.
Here are the core operator types that will be covered in this blog
Let's go first with Arithmetic Operators Arithmetic Operators are used to perform mathematical calculations,
Here is an example of Arithmetic Operators
let a = 30;
let b = 20;
console.log(a + b); // 50
console.log(a - b); // 10
console.log(a * b); // 600
console.log(a / b); // 1.5
console.log(a % b); // 10 ,it returns the remainder after division
Assignment Operators Assignment Operators are used to assign values to variables. for example:
in code
let a = 10 // 10
a += 5 // 15
a -= 5 // 10
a *= 2 // 20
a /= 2 // 10
a %= 5 // 0
** Comparison Operators **
Comparison Operators are used to compare two values in JavaScript, and return a Boolean value true or false depending upon the condition. Comparison Operators are mostly used in logical statements to determine equality.
Here are some Comparison Operators
in code
let price = 30
price > 40 // false
price < 40 // true
price >= 40 // false
price <= 40 // true
price == 40 // false
price === 40 // false
price != 40 // true
price !== 40 // true
A small doubt you may having that is what is the difference between == & ===, well, == compares only values (with type coercion), while === compares both value and type (no coercion).
let str = '1'
let num = 1
str == num // true
str === num // false
Logical Operators
Logical Operators are used to combine conditional statements, and return a boolean value true or false depending upon the condition. Logical Operators also help in creating complex if statements.
Here are some logical operators
in code
let price = 50
let discount = 10
price > 40 && discount > 5 // true
price > 40 || discount <= 10 // true
let isLoggedIn = false
!isLoggedIn // true
Hope you learn something new from this blog,