JavaScript Essentials - Operators
Detailed overview of operators in JavaScript
Operators are symbols used to perform operations on operands (values and variables). This tutorial provides a detailed overview of various operators in JavaScript, including arithmetic, assignment, comparison, logical, and more.
Key Points:
- Operators perform operations on one or more operands.
- JavaScript supports various types of operators.
- Understanding operators is essential for performing calculations and making decisions in code.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations. Here are some common arithmetic operators:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulus)++
(Increment)--
(Decrement)
var a = 10;
var b = 5;
console.log(a + b); // Output: 15
console.log(a - b); // Output: 5
console.log(a * b); // Output: 50
console.log(a / b); // Output: 2
console.log(a % b); // Output: 0
a++;
console.log(a); // Output: 11
b--;
console.log(b); // Output: 4
Assignment Operators
Assignment operators are used to assign values to variables. Here are some common assignment operators:
=
(Assignment)+=
(Addition assignment)-=
(Subtraction assignment)*=
(Multiplication assignment)/=
(Division assignment)%=
(Modulus assignment)
var a = 10;
a += 5; // Equivalent to a = a + 5
console.log(a); // Output: 15
a -= 2; // Equivalent to a = a - 2
console.log(a); // Output: 13
a *= 3; // Equivalent to a = a * 3
console.log(a); // Output: 39
a /= 3; // Equivalent to a = a / 3
console.log(a); // Output: 13
a %= 3; // Equivalent to a = a % 3
console.log(a); // Output: 1
Comparison Operators
Comparison operators are used to compare values and return a boolean result. Here are some common comparison operators:
==
(Equal to)===
(Strict equal to)!=
(Not equal to)!==
(Strict not equal to)>
(Greater than)>=
(Greater than or equal to)<
(Less than)<=
(Less than or equal to)
var a = 10;
var b = 5;
var c = '10';
console.log(a == c); // Output: true
console.log(a === c); // Output: false
console.log(a != b); // Output: true
console.log(a !== c); // Output: true
console.log(a > b); // Output: true
console.log(a >= b); // Output: true
console.log(a < b); // Output: false
console.log(a <= b); // Output: false
Logical Operators
Logical operators are used to combine or invert boolean values. Here are some common logical operators:
&&
(Logical AND)||
(Logical OR)!
(Logical NOT)
var a = true;
var b = false;
console.log(a && b); // Output: false
console.log(a || b); // Output: true
console.log(!a); // Output: false
console.log(!b); // Output: true
Other Operators
JavaScript also supports other operators, such as:
typeof
(Type of an operand)instanceof
(Instance of an object)?:
(Ternary/Conditional operator)
var a = 10;
var b = 'Hello';
console.log(typeof a); // Output: number
console.log(typeof b); // Output: string
var date = new Date();
console.log(date instanceof Date); // Output: true
var age = 20;
var status = (age >= 18) ? 'Adult' : 'Minor';
console.log(status); // Output: Adult
Summary
In this tutorial, you learned about various operators in JavaScript, including arithmetic, assignment, comparison, logical, and other operators. Understanding these operators is essential for performing calculations, making decisions, and manipulating data in your JavaScript programs.