Mastering JavaScript Operators: A Comprehensive Guide to Operators in JavaScript
In JavaScript, operators are keywords or symbol that perform some kind of operations on the operands. Operands are values or expressions on which operations are performed. Operators allow you to manipulate and control value for calculation purposes. They allow you to make comparisons and control the flow of your code.
Arithmetic Operators
They are operators that perform arithmetic operations like addition, subtraction, multiplication and more on numeric values.
let numOne = 4
let numTwo = 3
let sum = numOne + numTwo // 7
let diff = numOne - numTwo // 1
let mult = numOne * numTwo // 12
let div = numOne / numTwo // 1.33
let remainder = numOne % numTwo // 1
let powerOf = numOne ** numTwo // 64
console.log(sum, diff, mult, div, remainder, powerOf)
Assignment Operators
They assign values to the variables, either by declaring literals( numeric constants) or is combined with arithmetic operators to assign value based on previous values.
let num1 = 4,num2= 3 ,num3 = 5 , num4= 2
num1 += 2 // num1 = num1 + 2
num2 -= 1 // num2 = num2 -1
num3 *= 2 // num3 = num3 * 2
num4 /= 2 // num4 = num4 / 2
Comparison Operators
We use comparison operators to compare two values. We check if a value is greater or less or equal to other value.
console.log(3 > 2) // true, because 3 is greater than 2
console.log(3 >= 2) // true, because 3 is greater than 2
console.log(3 < 2) // false, because 3 is greater than 2
console.log(2 < 3) // true, because 2 is less than 3
console.log(2 <= 3) // true, because 2 is less than 3
console.log(3 == 2) // false, because 3 is not equal to 2
console.log(3 != 2) // true, because 3 is not equal to 2
Logical Operators
Logical operators are used to perform logical operations like AND, OR or NOT on multiple expressions and return a single boolean value. They are mostly used in conditionals to control flow of the code.
// &&(AND) ampersand operator example
const check = 4 > 3 && 10 > 5 // true && true -> true
const check = 4 > 3 && 10 < 5 // true && false -> false
const check = 4 < 3 && 10 < 5 // false && false -> false
// || (OR) pipe operator, example
const check = 4 > 3 || 10 > 5 // true || true -> true
const check = 4 > 3 || 10 < 5 // true || false -> true
const check = 4 < 3 || 10 < 5 // false || false -> false
// ! (NOT) Negation examples
let check = 4 > 3 // true
let check = !(4 > 3) // false
Unary Operators
Unary operators perform increment or decrement operation on a single operand. They increase or decrease the value of the variable. They are usually used inside loops.
let a = 2;
let b = 3;
a++ // a = a + 1 | Value of a is 3 now
b-- // b = b - 1 | Value of b is 2 now
String Concatenation Operator
The plus(+) only adds numeric values if both operands are numbers. Unlike other programming languages, JavaScript uses this sign to convert the type of variable from string to number. In other languages, the compiler throws an error. The sign is also used to concatenate or join two variables of similar or different data type.
let num1 = 5;
let num2 = "10";
let sum = num1 + +num2; //first + adds while secod + converts string to number
console.log(sum); // Output: 15 (numeric addition)
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
console.log(fullName); // Output: "John Doe"
console.log("2" + 2); // 22
Ternary Operator
Ternary operator works as inline if-else statement. It is used to determine the value of a variable based on certain conditions.
let age = 18;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // Output: "Adult"
This concludes the blog learning the basic usage of operators in JavaScript language.