Operators in JavaScript are symbols that perform operations on operands (variables, values, or expressions). They are classified into different categories based on their functionality. Here's an overview of the main types of operators in JavaScript:
Arithmetic Operators:
javascriptvar a = 10;
var b = 5;
var sum = a + b; // Addition
var difference = a - b; // Subtraction
var product = a * b; // Multiplication
var quotient = a / b; // Division
var remainder = a % b; // Modulus
var exponentiation = a ** b; // Exponentiation (ES7)
Assignment Operators:
javascriptvar x = 10; // Assign value 10 to variable x
var y = 5;
y += 3; // Equivalent to: y = y + 3 (Addition assignment)
Comparison Operators:
javascriptvar a = 10;
var b = 5;
var isEqual = a == b; // Equal to
var isNotEqual = a != b; // Not equal to
var isGreater = a > b; // Greater than
var isLess = a < b; // Less than
var isGreaterOrEqual = a >= b; // Greater than or equal to
var isLessOrEqual = a <= b; // Less than or equal to
Logical Operators:
javascriptvar x = true;
var y = false;
var andResult = x && y; // Logical AND
var orResult = x || y; // Logical OR
var notResult = !x; // Logical NOT
Unary Operators:
javascriptvar x = 10;
var y = -x; // Unary negation (-)
var z = ++x; // Increment (prefix)
var w = x--; // Decrement (postfix)
String Concatenation Operator:
+
operator can also be used to concatenate strings.javascriptvar firstName = "John";
var lastName = "Doe";
var fullName = firstName + " " + lastName; // Concatenation
Conditional (Ternary) Operator:
if-else
statement.javascriptvar age = 20;
var status = (age >= 18) ? "Adult" : "Minor"; // Ternary operator
Operators are fundamental to JavaScript programming, allowing you to perform various operations and make decisions in your code. Understanding how to use operators effectively is essential for writing efficient and concise JavaScript programs.
Thanks for learning