Operators in JavaScript
Introduction:
Types of Operators in JavaScript:
JavaScript has several types of Operators, including:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Ternary Operator
Arithmetic operators:
In JavaScript, arithmetic operators are used to performing mathematical operations such as addition, subtraction, multiplication, and division. There are several different arithmetic operators available in JavaScript, including:
Operator |
Function |
Example |
+ (addition) |
This operator is used to add two
numbers or concatenate two strings. |
let x = 3 + 4; // x will be 7 let y = "Hello" +
"world"; // y will be "Helloworld" |
- (subtraction)
|
This operator is used to subtract one
number from another |
let x = 10 - 5; // x will be 5 |
* (multiplication)
|
This operator is used to multiply two
numbers |
let x = 3 * 4; // x will be 12 |
/ (division)
|
This operator is used to divide one
number by another |
let x = 8 / 4; // x will be 2 |
% (modulus)
|
This operator is used to find the
remainder of a division operation. |
let x = 10 % 3; // x will be 1 |
** (Exponentiation) |
It raises the first number to the
power of the second number. |
let x = 5; let y = 2; console.log(x ** y); // prints "25" |
++ |
This operator is used to increment a
number by 1. |
let x = 5; x++; // x will be 6 |
-- |
This operator is used to decrement a
number by 1. |
let x = 5; x--; // x will be 4 |
These operators can be used in a variety of ways in JavaScript, including in expressions, assignments, and even as standalone statements. For example:
let x = 5;
let y = 3;
let z = x + y; // z will be 8
x += 2; // x will be 7
y *= 4; // y will be 12
Assignment operators:
Operator |
Function |
Example |
= |
It is used to assign the value to a
variable |
let x = 5; // x is now 5 |
+= |
This operator is used to add a value
to a variable and assign the result to the variable |
let x = 5; x += 3; // x is now 8 |
-= |
This operator is used to subtract a
value from a variable and assign the result to the variable. |
let x = 5; x -= 3; // x is now 2 |
*= |
This operator is used to multiply a
variable by a value and assign the result to the variable |
let x = 5; x *= 3; // x is now 15 |
/= |
This operator is used to divide a
variable by a value and assign the result to the variable |
let x = 15; x /= 3; // x is now 5 |
%= |
This operator is used to calculate the
remainder of dividing a variable by a value and assign the result to the
variable |
let x = 10; x %= 3; // x is now 1 |
Comparison operators:
In JavaScript, comparison operators are used to comparing two values and return a Boolean value indicating whether the comparison is true or false. The comparison operators in JavaScript include:
Operator |
Function |
Example |
== |
This operator is used to test for
equality between two values. It returns true if the values are equal, and
false if they are not |
console.log(5 == 5); // prints true console.log(5 == "5"); //
prints true console.log(5 == 6); // prints false |
!= |
This operator is used to test for
inequality between two values. It returns true if the values are not equal,
and false if they are. |
console.log(5 != 5); // prints false console.log(5 != "5"); //
prints false console.log(5 != 6); // prints true |
> |
This operator is used to test if the
value on the left is greater than the value on the right. It returns true if
the left value is greater, and false if it is not. |
console.log(5 > 5); // prints false console.log(5 > 4); // prints true |
< |
This operator is used to test if the
value on the left is less than the value on the right. It returns true if the
left value is less, and false if it is not. |
console.log(5 < 5); // prints false console.log(5 < 6); // prints true |
>= |
This operator is used to test if the
value on the left is greater than or equal to the value on the right. It
returns true if the left value is greater or equal, and false if it is not. |
console.log(5 >= 5); // prints true console.log(5 >= 6); // prints
false |
<= |
This operator is used to test if the
value on the left is less than or equal to the value on the right. It returns
true if the left value is less or equal, and false if it is not. |
console.log(5 <= 5); // prints true console.log(5 <= 4); // prints
false |
These operators can be very useful for
writing conditional statements and performing different actions based on the
result of a comparison. Example: let x = 5; if (x > 3) {
console.log("x is greater than 3"); } else {
console.log("x is not greater than 3"); } |
Logical operators:
In JavaScript, logical operators are used to perform logical operations on Boolean values. The logical operators in JavaScript include:
Operator |
Function |
Example |
&& |
This operator is used to test if both
of the values on either side of it are true. It returns true if both values
are true, and false if either value is false. |
console.log(true && true); //
prints true console.log(true && false); //
prints false console.log(false && true); //
prints false console.log(false && false);
// prints false |
|| |
This operator is used to test if
either of the values on either side of it are true. It returns true if either
value is true, and false if both values are false. |
console.log(true || true); // prints
true console.log(true || false); // prints
true console.log(false || true); // prints
true console.log(false || false); // prints
false |
! |
This operator is used to negate a
boolean value. It returns true if the value is false, and false if the value
is true. |
console.log(!true); // prints false console.log(!false); // prints true |
Conditional (ternary) operator:
In JavaScript, the conditional (ternary) operator is a special operator that is used to perform a conditional operation based on the value of a Boolean expression. It is also known as the ternary operator because it takes three operands: a Boolean expression followed by a question mark (?), and two expressions console.log(y); // prints "x is greater than 3"
For example:
let x = 5;
let y = (x > 3) ? "x is greater than 3" : "x is not greater than 3";
In this example, the boolean expression x > 3 evaluates to true, so the operator returns the string "x is greater than 3".
The conditional operator is often used as a concise way to write simple if-else statements.
String Operator:
In JavaScript, strings are a sequence of characters and are used to represent text. There are several string operators that you can use to manipulate strings in JavaScript. Some of the most commonly used string operators are:
Concatenation operator (+):This operator is used to concatenate two or more strings together.
For example:
let str1 = "Hello";
let str2 = "World";
let str3 = str1 + " " + str2; // "Hello World"
Template literals ( ):
This operator is used to concatenate multiple strings together in a more readable format, it uses backtick (`) to define the string.
For example:
let name = "John";
let age = 30;
let str = `My name is ${name} and I am ${age} years old.`; // "My name is John and I am 30 years old."
No comments:
Post a Comment