JavaScript is a high-level, interpreted programming language widely used in web development. It is a versatile language, and one of the key features of JavaScript is the use of operators. Operators in JavaScript are symbols used to perform operations on one or more values, such as arithmetic operations or logical comparisons. This article will cover various types of operators in JavaScript with examples.
- Arithmetic Operators Arithmetic operators are used to perform mathematical operations on numbers. The arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), modulo (%), and exponentiation (**).
Example:
let a = 10; let 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
console.log(a ** b); // output: 100000
- Assignment Operators Assignment operators are used to assign a value to a variable. The assignment operator (=) is the most common assignment operator used in JavaScript. Other assignment operators include +=, -=, *=, /=, %=, and **=.
Example:
let a = 10; let b = 5;
a += b; // equivalent to a = a + b console.log(a); // output: 15
a -= b; // equivalent to a = a - b console.log(a); // output: 10
a *= b; // equivalent to a = a * b console.log(a); // output: 50
a /= b; // equivalent to a = a / b console.log(a); // output: 10
a %= b; // equivalent to a = a % b console.log(a); // output: 0
a **= b; // equivalent to a = a ** b console.log(a); // output: 0
- Comparison Operators Comparison operators are used to compare two values and return a Boolean value (true or false). The comparison operators include ==, ===, !=, !==, >, >=, <, and <=.
Example:
let a = 10; let b = 5;
console.log(a == b); // output: false
console.log(a === b); // output: false
console.log(a != b); // output: true
console.log(a !== b); // 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 perform logical operations on Boolean values. The logical operators include &&, ||, and !.
Example:
let a = true; let b = false;
console.log(a && b); // output: false
console.log(a || b); // output: true
console.log(!a); // output: false
- Bitwise Operators Bitwise operators are used to perform operations on the binary representations of numbers. The bitwise operators include &, |, ^, ~, <<, and >>.
Example:
let a = 5; let b = 3;
console.log(a & b); // output: 1
console.log(a | b); // output: 7
console.log(a ^ b); // output: 6
console.log(~a); // output: -6
console.log(a << 1); // output: 10
console.log(a >> 1); // output: 2
- Conditional (Ternary) Operator The conditional operator (?:) is a shorthand way of writing an if...else statement. It takes three operands, and the syntax is:
condition ? expression1 : expression2;
If the condition is true, the expression1 is evaluated, otherwise, the expression2 is evaluated.
Example:
let a = 10; let b = 5;
let result = (a > b) ? "a is greater than b" : "b is greater than a";
console.log(result); // output: "a is greater than b"
- String Operators String operators are used to concatenate or combine strings. The concatenation operator (+) is used to concatenate two or more strings.
Example:
let str1 = "Hello"; let str2 = "World";
console.log(str1 + " " + str2); // output: "Hello World"
- Type Operators Type operators are used to check the type of a value. The typeof operator is used to determine the type of a value.
Example:
let a = 10; let b = "Hello";
console.log(typeof a); // output: "number"
console.log(typeof b); // output: "string"
- Object Operators Object operators are used to access and manipulate the properties and methods of an object. The dot (.) operator is used to access the properties and methods of an object.
Example:
let person = {
firstName: "John", lastName: "Doe", age: 30, fullName: function() {
return this.firstName + " " + this.lastName; } };
console.log(person.firstName); // output: "John"
console.log(person.lastName); // output: "Doe"
console.log(person.age); // output: 30
console.log(person.fullName()); // output: "John Doe"
- Spread Operator The spread operator (...) is used to spread an array or object into individual elements. It is commonly used to concatenate arrays or objects.
Example:
let arr1 = [1, 2, 3]; let arr2 = [4, 5, 6];
let arr3 = [...arr1, ...arr2];
console.log(arr3); // output: [1, 2, 3, 4, 5, 6]
- Nullish Coalescing Operator The nullish coalescing operator (??) is used to check if a value is null or undefined, and return a default value if it is. It returns the left-hand operand if it is not null or undefined, otherwise it returns the right-hand operand.
Example:
let a = null; let b = 5;
console.log(a ?? b); // output: 5
- Optional Chaining Operator The optional chaining operator (?.) is used to access a property or method of an object if it exists, without causing an error if it does not. It is commonly used to avoid "undefined" errors.
Example:
let person = { firstName: "John", lastName: "Doe", age: 30, address: { city: "New York", state: "NY" } };
console.log(person.address?.city); // output: "New York"
console.log(person.address?.zipCode); // output: undefined
Conclusion Operators are an essential part of programming, and JavaScript provides a wide range of operators to perform various operations. In this article, we have covered various types of operators in JavaScript with examples. Understanding these operators will help you write efficient and effective JavaScript code.