JavaScript has several basic operators that can be used to perform arithmetic, comparison, logical, and assignment operations. Here are some of the most commonly used operators:
Arithmetic Operators:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exponentiation (**)
Comparison Operators:
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
Logical Operators:
- AND (&&)
- OR (||)
- NOT (!)
Assignment Operators:
- Assign (=)
- Add and assign (+=)
- Subtract and assign (-=)
- Multiply and assign (*=)
- Divide and assign (/=)
- Modulus and assign (%=)
Ternary Operator:
- Ternary operator (a ? b : c)
These are some of the basic operators in JavaScript that are essential for performing basic operations and building more complex logic.
JavaScript is a programming language that uses several operators to perform different tasks on values and variables. Here are some basic operators in JavaScript with examples:
- Arithmetic operators: Arithmetic operators perform mathematical operations on numerical values.
Example:
javascript
let x = 5; let y = 2; console.log(x + y); // Output: 7 console.log(x - y); // Output: 3 console.log(x * y); // Output: 10 console.log(x / y); // Output: 2.5 console.log(x % y); // Output: 1
- Assignment operators: Assignment operators assign a value to a variable.
Example:
javascript
let x = 5; x += 3; // Same as x = x + 3 console.log(x); // Output: 8
- Comparison operators: Comparison operators compare two values and return a Boolean value (true or false).
Example:
javascript
let x = 5; let y = 2; console.log(x > y); // Output: true console.log(x < y); // Output: false console.log(x == y); // Output: false console.log(x != y); // Output: true console.log(x >= y); // Output: true console.log(x <= y); // Output: false
- Logical operators: Logical operators are used to combine two or more conditions and return a Boolean value.
Example:
javascript
let x = 5; let y = 2; console.log(x > 3 && y < 5); // Output: true console.log(x > 3 || y > 5); // Output: true console.log(!(x > y)); // Output: false
- String operators: String operators are used to concatenate strings.
Example:
javascript
let firstName = "John"; let lastName = "Doe"; console.log(firstName + " " + lastName); // Output: "John Doe"
- Conditional (ternary) operator: The conditional (ternary) operator is used to assign a value to a variable based on a condition.
Example:
javascript
let age = 20; let status = (age >= 18) ? "adult" : "minor"; console.log(status); // Output: "adult"


0 Comments