JavaScript operators are used to carry out different types of mathematical and logical calculations.
Types of JavaScript Operators
JavaScript provides various types of operators, including:
- Arithmetic Operators: Perform mathematical calculations.
- Assignment Operators: Assign values to variables.
- Comparison Operators: Compare values to return a boolean result.
- String Operators: Handle string concatenation and manipulation.
- Logical Operators: Evaluate logical conditions.
- Bitwise Operators: Perform bit-level operations.
- Ternary Operators: Simplify conditional expressions.
- Type Operators: Determine or work with data types.
JavaScript Arithmetic Operators
Arithmetic Operators are utilized to execute mathematical operations on numbers.
Addition
The Addition Operator (+) is used to add numeric values.
let a = 8;
let b = 4;
let c = a + b; // Output: 12
Multiplication
The Multiplication Operator (*) is used to perform the mathematical operation of multiplying numbers.
let a = 8;
let b = 4;
let c = a * b; // Output: 32
Addition and Multiplication
let a = 5;
let b = (50 + 20) * a; // Output: 350
More Arithmetic Operators
+
- Adds two numbers together-
- Subtracts one number from another*
- Multiplies two numbers**
- Raises a number to the power of another (ES2016)/
- Divides one number by another%
- Returns the remainder of a division++
- Increments a number by 1--
- Decrements a number by 1
JavaScript Assignment Operators
The Assignment Operator (=) is used to assign a value to a variable.
Example:
// Assign the value 10 to a
let a = 10;
// Assign the value 3 to b
let b = 3;
// Assign the value a + b to c
let c = a + b;
The Addition Assignment Operator (+=) increases the value of a variable by a specified amount.
let a = 5;
a += 2; // Output: 7
More Assignment Operators
=
- x = y: Assigns the value of y to x+=
- x += y: Adds y to x and assigns the result to x-=
- x -= y: Subtracts y from x and assigns the result to x*=
- x *= y: Multiplies x by y and assigns the result to x/=
- x /= y: Divides x by y and assigns the result to x%=
- x %= y: Finds the remainder of x divided by y and assigns it to x**=
- x **= y: Raises x to the power of y and assigns the result to x
JavaScript Comparison Operators
Comparison operators in JavaScript are used to compare values and return a Boolean (true or false).
String Comparison
All comparison operators can be applied to strings.
Example
let str1 = "A";
let str2 = "B";
let comparisonResult = str1 < str2; // Output: true
Keep in mind that strings are compared in alphabetical order.
let str1 = "10";
let str2 = "20";
let comparisonResult = str1 < str2; // Output: true
More Comparison Operators
==
- Checks if values are equal (type conversion allowed)===
- Checks if values and types are both equal!=
- Checks if values are not equal!==
- Checks if values or types are not equal>
- Checks if the left value is greater than the right value<
- Checks if the left value is less than the right value>=
- Checks if the left value is greater than or equal to the right value<=
- Checks if the left value is less than or equal to the right value?
- Represents the ternary operator for conditional expressions
String Operators
String operators are used to manipulate and compare strings in programming.
String Addition
The + operator can also be used to concatenate (combine) strings.
let firstName = "William";
let lastName = "David";
let fullName = firstName + " " + lastName; // Output: William David
Adding Strings and Numbers
When adding two numbers, the result will be their sum. However, if you add a number and a string, the result will be a concatenated string.
let a = 10 + 10; // Output:20
let b = "10" + 15; // Output:1015
let c = "Greetings" + 20; // Output: Greetings20
Note: When you add a number and a string, the result will always be a string!
JavaScript Logical Operators
JavaScript logical operators are used to perform boolean logic operations, often in conditional statements.
Logical AND (&&)
Both conditions must be true for the result to be true.
console.log(5 > 2 && 10 > 20); // Output: false
console.log(5 > 2 && 10 < 20); // Output: true
Logical OR (||)
At least one condition must be true for the result to be true.
console.log(5 > 10 || 20 > 15); // Output: true
console.log(5 > 10 || 20 > 15); // Output: true
Logical NOT (!)
Reverses the boolean value (true becomes false and vice versa).
console.log(!(5 < 10)); // Output: false
console.log(!(5 > 10)); // Output: true
JavaScript Type Operators
JavaScript type operators are used to check or manipulate data types.
typeof
– Returns the type of a variable as a string.
console.log(typeof 42); // "number"
console.log(typeof "Hello"); // "string"
console.log(typeof true); // "boolean"
instanceof
– Checks if an object is an instance of a specific class or constructor.
console.log([] instanceof Array); // true
console.log({} instanceof Object); // true
console.log("Hello" instanceof String); // false (because "Hello" is a primitive, not an object)