JavaScript Operators

CodingTute

JavaScript

In JavaScript, operators are symbols that perform specific operations on one or more values (also known as operands) and produce a result. There are several types of operators in JavaScript, including:

  1. Arithmetic operators: These operators perform basic arithmetic operations, such as addition, subtraction, multiplication, and division. For example: +, -, *, /.
  2. Assignment operators: These operators assign a value to a variable. The most basic assignment operator is =, which assigns the value of the right operand to the left operand. There are also compound assignment operators, such as +=, -=, *=, and /=, which perform an operation and assign the result to the left operand in one step.
  3. Comparison operators: These operators compare two values and return a boolean value indicating whether the comparison is true or false. For example: ==, !=, >, <, >=, <=.
  4. Logical operators: These operators perform logical operations, such as AND (&&) and OR (||). They are often used in conjunction with comparison operators to create more complex conditions.
  5. Unary operators: These operators perform an operation on a single operand. For example, the unary negation operator (-) negates a number, and the unary not operator (!) inverts a boolean value.
  6. Ternary operator: This operator is also known as the conditional operator (? :). It takes three operands and evaluates a boolean condition. If the condition is true, the operator returns the value of the second operand; if the condition is false, it returns the value of the third operand.

Here are some examples of operator usage in JavaScript:

let x = 10;
let y = 20;

// Arithmetic operators
console.log(x + y);  // 30
console.log(x - y);  // -10
console.log(x * y);  // 200
console.log(x / y);  // 0.5

// Assignment operator
let z = x + y;  // z is now 30

// Comparison operators
console.log(x == y);  // false
console.log(x != y);  // true
console.log(x > y);  // false
console.log(x < y);  // true

// Logical operators
console.log(x > 0 && y > 0);  // true
console.log(x > 0 || y > 0);  // true

// Unary operators
console.log(-x);  // -10
console.log(!true);  // false

// Ternary operator
let max = (x > y) ? x : y;  // max is now 20

You can find the complete JavaScript Tutorials here.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.