.Js
Operators
Operators in JavaScript are fundamental building blocks used to perform operations on variables and values. Here are some of the basic operators in JavaScript:
Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations on numerical values. They include addition, subtraction, multiplication, division, modulus (remainder), increment, and decrement.
let a = 10; let b = 5; let addition = a + b; // Addition let subtraction = a - b; // Subtraction let multiplication = a * b; // Multiplication let division = a / b; // Division let modulus = a % b; // Modulus (remainder of division) let increment = ++a; // Increment let decrement = --b; // Decrement
Operators | Meanings | Examples | Results |
---|---|---|---|
+ | Addition | 5+3 | 8 |
- | Subtraction | 5-3 | 2 |
* | Multiplication | 5*3 | 15 |
/ | Division | 6/3 | 2 |
% | Modulus (Remainder) | 6%4 | 2 |
++ | Increment | let x = 5; x++; | x = 6 |
-- | Decrement | let y = 5; y--; | y = 4 |
Assignment Operators:
Assignment operators are used to assign values to variables. They can also perform arithmetic operations and assign the result to the variable in a concise way.
let x = 10; x += 5; // Equivalent to x = x + 5;
Operators | Meanings | Examples | Results |
---|---|---|---|
= | Assignment | let a = 5; | a = 5 |
+= | Addition assignment | let b = 3; b += 2; | b = 5 |
-= | Subtraction assignment | let c = 7; c -= 4; | c = 3 |
*= | Multiplication assignment | let d = 4; d *= 3; | d = 12 |
/= | Division assignment | let e = 6; e /= 2; | e = 3 |
%= | Modulus assignment | let f = 5; f %= 2; | f = 1 |
Comparison Operators:
Comparison operators are used to compare two values or expressions. They return a Boolean value ('true' or 'false') based on the comparison result. These operators include equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to.
let a = 10; let b = 5; console.log(a == b); // Equal to console.log(a != b); // Not equal to console.log(a > b); // Greater than console.log(a < b); // Less than console.log(a >= b); // Greater than or equal to console.log(a <= b); // Less than or equal to
false true true false true false
Operators | Meanings | Examples | Results |
---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
=== | Strict equal to | 5 === "5" | false |
!== | Strict not equal to | 5 !== "5" | true |
> | Greater than | 7 > 4 | true |
< | Less than | 3 < 6 | true |
>= | Greater than or equal to | 5 >= 5 | true |
<= | Less than or equal to | 4 <= 4 | true |
Logical Operators:
Logical operators are used to combine multiple conditional statements. They are typically used in conjunction with comparison operators to create complex conditions. Logical operators include AND ('&&'), OR ('||'), and NOT ('!').
let x = 10; let y = 5; let z = 15; console.log(x > y && x < z); // Logical AND console.log(x > y || x > z); // Logical OR console.log(!(x > y)); // Logical NOT
true true false
Operators | Meanings | Examples | Results |
---|---|---|---|
&& | Logical AND | (5 > 3) && (4 < 7) | true |
|| | Logical OR | (5 > 3) || (4 > 7) | true |
! | Logical NOT | !(5 > 3) | false |
String Operators:
The string concatenation operator ('+') is used to concatenate two or more strings together.
let firstName = "Ayan"; let lastName = "Sarkar"; let fullName = firstName + " " + lastName;
Operators | Meanings | Examples | Results |
---|---|---|---|
+ | Concatenation | "Hello, " + "world!" | Hello, world! |
Unary Operators:
Unary operators work on a single operand. They include unary plus ('+'), unary minus ('-'), increment ('++'), decrement ('--'), and logical NOT ('!'). They are often used for mathematical operations or to invert the truth value of an expression.
let x = 10; let y = -x; // Unary minus let z = +x; // Unary plus
Operators | Meanings | Examples | Results |
---|---|---|---|
+ | Unary plus | +5 | 5 |
- | Unary minus | -5 | -5 |
! | Logical NOT | !(5 > 3) | false |
typeof | Typeof | typeof "Hello" | "string" |
Note: The 'typeof' operator, specifically, returns a string indicating the type of the operand.
Ternary Operator:
The ternary operator ('? :') is a concise way of writing conditional statements. It evaluates a condition and returns one of two values depending on whether the condition is true or false.
let age = 20; let result = (age >= 18) ? "Senior" : "Minor";
Operators | Meanings | Examples | Results |
---|---|---|---|
? : | Ternary | let age = 20; (age >= 18) ? "Senior" : "Minor" | "Senior" or "Minor" |
Note: These operators are fundamental to JavaScript and are used extensively in programming to perform various tasks, including arithmetic operations, variable assignments, comparisons, and logical operations.
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.